improve inefficient jQuery selector

后端 未结 2 766
遥遥无期
遥遥无期 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.

    0 讨论(0)
  • 2021-02-08 15:39

    Try to use .find() here:

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

    Your warning seem like related to efficiency of the selector.

    Related thread

    0 讨论(0)
提交回复
热议问题