有没有更高效的方法来编写这个代码呢?
$('#test').find('option:selected[value!=""]')
有没有更高效的方法来编写这个代码呢?
$('#test').find('option:selected[value!=""]')
您可以稍加调整,但使用方法而不是Sizzle:
$('#test').find('option').filter(function() {
return this.selected && this.value.length
});
基准测试: http://jsperf.com/sizzle-vs-methods-filter/12
.filter()
对我来说快约70%。
return语句
,请看更新。抱歉。 - jAndy嗯,始终只会有一个被选中,所以我认为你不需要一个find()
处理程序。
我会这样写:
$('#test option:selected[value!=""]')
我还没有测试过。