开发者

How to manage the X element from a list of elements with the same class

I have these elements :

<div class="container">
    <div class="myTarget">a</div>
    <div class="notMyTarget">b</div>
    <div class="myTarget">c</div>
    <div class="notMyTarget">d</div>
    <div class="myTarget">e</div>
    <div class="myTarget">f</div>
    <div class="notMyTarget">g</div>
    <div class="myTarget">h</div>
    <div class="notMyTarget">i</div>
    <div class="notMyTarget">j</div>
    <div class="myTarget">k</div>
    <div class="myTarget">l</div>
    <div class="notMyTarget">m</div>
    <div class="myTarget"&g开发者_StackOverflow中文版t;n</div>
    <div class="myTarget">o</div>
</div>

and I'd like to remove (for example) the 4° elements of the myTarget's collections div that are children of container. So in this case, I'd like to remove the one with f.

What's the best and easy way to do it on jQuery? .index() can help?


Use the (zero-indexed) :eq() selector:

$('.container .myTarget:eq(3)').remove();

Counting up from 0, 3 represents the fourth .myTarget element.


$('.container div').eq(5).remove()

Where 5 is the index.


Try this

$(".container").find(".myTarget:eq(3)").remove();


Use the eq selector :

$('.container .myTarget:eq(3)').remove();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜