【发布时间】:2011-06-23 06:57:12
【问题描述】:
有没有更高效的方法来写这个。
$('#test').find('option:selected[value!=""]')
【问题讨论】:
-
完全没有,只是想看看有没有更好的写法。
标签: jquery performance jquery-selectors
有没有更高效的方法来写这个。
$('#test').find('option:selected[value!=""]')
【问题讨论】:
标签: jquery performance jquery-selectors
您可以稍微调整一下,但使用方法而不是 Sizzle:
$('#test').find('option').filter(function() {
return this.selected && this.value.length
});
基准测试:http://jsperf.com/sizzle-vs-methods-filter/12
.filter() 对我来说快了大约 70%。
【讨论】:
return statement,请参阅更新。对不起。
嗯,总是只有一个被选中,所以我认为你不需要find() 处理程序。
我就这样写吧:
$('#test option:selected[value!=""]')
我还没有测试过。
【讨论】: