开发者

jquery traversing dom

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜