2
<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ありselect2、オプションのみがあります21

children()

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

奇妙に動作します:select1オプションしかありません11select2、もうオプションはありません...

なんで?

デモ

4

2 に答える 2

3

.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/

これはバグかもしれませんが、さらに調査する必要があります。

于 2012-12-03T15:35:50.213 に答える
2

私が見たものから

$('#select1, #select2').find('option:not(:first)')

に等しい

$('#select1  option:not(:first), #select2  option:not(:first)')

いいえ

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

.find() を使用するのと同じであるため、コンテキストセレクターについて考えてください。

$('option:not(:first)',$('#select1, #select2'))

子を first.. で使用することにより、 collection.. で返される最初の子オプションのみを取得しますが、コンテキスト/検索セレクターは各コンテキストで最初のオプションを探します

于 2012-12-03T15:47:15.590 に答える