开发者

jquery traversing dom

开发者 https://www.devze.com 2023-04-11 00:03 出处:网络
How do I select the last div id blog_comments in this row? <div id=\"blog_comments\"> <div class=\"name\"><span class=\"blog_bold\">By: </span>SamSambinks</div>

How do I select the last div id blog_comments in this row?

    <div id="blog_comments">
        <div class="name"><span class="blog_bold">By: </span>SamSambinks</div>
    </div>

    <div id="blog_comments">
        <div class="name"><span class="blog_bold">By: </span>SamSambinks</div>
    </div>

    <div id="blog_comments">
        <div class="name"><span class="blog_bold">By: </span>SamSambinks</div>
    </div>

Sounds like a simple question I've tried: $("#blog_comments:last") and $("#blog_comments").last

and others but n开发者_如何转开发othing seems to work?


The reason why it's not working is id needs to be unique. Make them classes instead of creating non-unique id's, having duplicate id's is actually invalid markup.

<div class="blog_comments">
   <div class="name"><span class="blog_bold">By: </span>SamSambinks</div>
</div>

<div class="blog_comments">
   <div class="name"><span class="blog_bold">By: </span>SamSambinks</div>
</div>

<div class="blog_comments">
   <div class="name"><span class="blog_bold">By: </span>SamSambinks</div>
</div>

Then your selector:

$('.blog_comments').last();

Working Example here


You can't have multiple DOM nodes with same id. Try to change id="blog_comments" to class="blog_comments".

0

精彩评论

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