<select id="select1">
<option value="11">11</option>
<option value="12">12</option>
</select>
<select id="select2">
<option value="21">21</option>
<option value="22">22</option>
</select>
的行为find()
和children()
方法:
find()
:
$('#select1, #select2').find('option:not(:first)').remove();
按预期工作:select1
仅有的选择11
and select2
仅有的选择21
children()
:
$('#select1, #select2').children('option:not(:first)').remove();
工作起来很奇怪:select1
仅有的选择11
but select2
已经没有选择了...
Why?
Demo http://jsfiddle.net/tyZzy/
我无法解释为什么.find
正在与:first
,但是.children
不与:first
因为:first
获取选定元素集中的第一个选定元素,而不是第一个子元素。你想要的是:first-child
.
// children
$('#select1, #select2').children('option:not(:first-child)').remove();
// find
$('#select3, #select4').find('option:not(:first-child)').remove();
Demo: http://jsfiddle.net/tyZzy/2/ http://jsfiddle.net/tyZzy/2/
这可能是一个错误,尽管需要更多研究。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)