在回答以下jQuery问题Need help in Optimizing the below jquery code的过程中,我偶然发现了另一个关于.find()
和.children()
的问题。jQuery与find()和children()结合使用时的区别:第一个
的问题是,授予了四种selectboxes与IDS 状态,城市,分支,branchAddress,删除所有,但每一个选择框的第一个选项。
已经发布了几个答案。在这些有:
$('#state,#city,#branch,#branchAddress').children('option:not(:first)').remove();
$('#state,#city,#branch,#branchAddress').children('option:not(:first-child)').remove();
$('#state,#city,#branch,#branchAddress').find('option:not(:first)').remove();
解决方案1似乎不工作(删除所有的选项,除了第一个选择框的第一个选项)根据本js小提琴(http://jsfiddle.net/QkNRf/1/)
解决方案2和3似乎完美地工作。
如果有人能够指出我错过了什么,或者向我解释为什么解决方案3在解决方案1不适用的情况下工作,我会很高兴。
这是正确答案,请参阅:http://jsfiddle.net/QkNRf/2/ –
thx也@roasted!我想.children()文件有点误导。大多数人似乎认为与.find()唯一的区别在于搜索深度。 – jbl