jQuery 的多重选择器 + find() 与 Children()

标签 jquery jquery-selectors find children

<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 仅具有选项 11select2 仅具有选项 21

children():

$('#select1, #select2').children('option:not(:first)').remove();

工作很奇怪:select1只有选项11,但select2不再有选项...

为什么?

Demo

最佳答案

我无法解释为什么 .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();​

演示:http://jsfiddle.net/tyZzy/2/

这可能是一个错误,但需要更多研究。

关于jQuery 的多重选择器 + find() 与 Children(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13686415/

相关文章:

javascript - jQuery 选择器将所有脚本作为文本关闭页面

bash - 为什么这个变量在查找迭代中没有改变它的值

javascript - deviceready 未在phonegap中触发

jQuery 添加事件监听器

JQuery 返回具有空属性选择器的非空输入

javascript - 如何选择从一个点给定半径内的所有元素?

Python检查字符串是否包含字典键

matlab - 如何在Matlab中找到最小10%的数据?

javascript - 如何使用 html2canvas 打印 Canvas

javascript - 无法停止 JavaScript 中的提交事件