Jquery element+class selector performance

后端 未结 4 936
萌比男神i
萌比男神i 2020-12-12 18:09

I was hoping $(\'#childDiv2 .txtClass\') or $(\'#childDiv2 input.txtClass\') perform better when selecting

4条回答
  •  情歌与酒
    2020-12-12 18:53

    CSS Selectors are parsed from right to left. So your example

    $('#childDiv2 .txtClass')
    

    will take two actions to complete. First find all elements with class txtClass. Then check each element for being a child of the element with the id childDiv2.

    $('.txtClass')
    

    This selector will just take one action. Find all elements with class txtClass

    Have a look at this article on css-tricks.com

提交回复
热议问题