improve inefficient jQuery selector

后端 未结 2 886
遥遥无期
遥遥无期 2021-02-08 14:58

In IntelliJ, if I use a jQuery selector such as:

$(\'#roleField option\').each(function() {
    // impl omitted
});

The selector is highlighted

2条回答
  •  春和景丽
    2021-02-08 15:29

    From the jquery documentation this method will not go through the Sizzle sector engine:

    $('#roleField option').each(function() {
        // No Sizzle
    }); 
    

    Where this one will:

    $('#roleField').find('option')  // Sizzle!
    

    Look at the ID base selectors section here. Hence the second method will be faster than the first.

提交回复
热议问题