jQuery选择器优化问题,使用$('#a>.b'),IDE会提示这是低效的用法?


使用 $('#a>.b') ,提示:
Checks that jQuery selectors are used in an efficient way. It suggests to split descendant selectors which are prefaced with ID selector and warns about duplicated selectors which could be cached.

没看懂它说要干嘛,只看懂了说要把有id选择器为前缀的后代选择器分开来,然后说要缓存...那到底是要怎么分开来? $('#a').find('.b') 这样吗?缓存的话,我只用到一次啊,没必要缓存啊

jquery webstorm JavaScript

Aoist 10 years ago

 $('#a>.b')

如果浏览器不支持document.querySelector()那么jquery就会利用sizzle模块切割选择器在用find方法查询。


 $('#a').find('.b')

直接find查询

个人见解,有误请指正。

yampoo answered 10 years ago


 php


 var a = $('#a');//缓存
var a = a.find('.b');

回来家结婚 answered 10 years ago

Your Answer