一致するすべての要素を削除したいのですが、各一致の最初のインスタンスをスキップします。
// Works as expected: removes all but first instance of .a
jQuery ('.a', '#scope')
.each ( function (i) {
if (i > 0) jQuery (this).empty();
});
// Expected: removal of all but first instance of .a and .b
// Result: removal of *all* instances .a and .b
jQuery ('.a, .b', '#scope')
.each ( function (i) {
if (i > 1) jQuery (this).empty();
});
<div id="scope">
<!-- Want to keep the first instance of .a and .b -->
<div class="a">[bla]</div>
<div class="b">[bla]</div>
<!-- Want to remove all the others -->
<div class="a">[bla]</div>
<div class="b">[bla]</div>
<div class="a">[bla]</div>
<div class="b">[bla]</div>
...
</div>
助言がありますか?
- 「レガシー」コードとの競合のためで
jQuery()
はなく使用$()
- 無効にしたいJSが含まれている
.empty()
ため使用.a
- jQuery 1.2.3で立ち往生
ありがとうございます!