我正在使用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();
任何人都可以解释原因吗?可能的锻炼或替代实施的想法。
由于
答案 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;