jquery selector performance

放肆的年华 提交于 2019-12-13 14:19:16

问题


Is there a more performant way to write this.

$('#test').find('option:selected[value!=""]')

回答1:


You can tweak it at little, but using methods instead of Sizzle:

$('#test').find('option').filter(function() {
    return this.selected && this.value.length
});

Benchmark: http://jsperf.com/sizzle-vs-methods-filter/12

.filter() is about 70% faster for me.




回答2:


Well, there will always only be one selected, so you don't need a find() handler in my opinion.

I'll just write it like this:

$('#test option:selected[value!=""]')

I haven't tested it yet.



来源:https://stackoverflow.com/questions/4930946/jquery-selector-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!