让孩子们通过jQuery中的id和class

时间:2015-01-14 20:36:25

标签: jquery

我正在使用id来获取孩子,现在我改为使用课程。这样做,我发现使用课堂并没有让孩子们回归。

工作代码

//Don't remove first element of select box with id 'someSelectBox'    
$('#someSelectBox').children('option:not(:first)').remove();

不工作代码

//Don't remove first element of ALL select box with class 'selectBoxClass'
$('.selectBoxClass').children('option:not(:first)').remove();

任何人都可以解释原因吗?可能的锻炼或替代实施的想法。

由于

1 个答案:

答案 0 :(得分:2)

如果您要排除每个option元素的第一个select,则需要使用.children('option:not(:first-child)')
因为您定位了多个select元素而:first仅适用于整个结果集中的第一个

否则,您只会排除第一个option元素的第一个select


课程应该没有问题



var options = $('.select-to-include').children('option:not(:first-child)');

options.css({color:'red'});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<select class="select-to-include">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>
<select class="select-to-include">
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>
&#13;
&#13;
&#13;