使用特定的类名在DOM中查找最后一个span

时间:2012-07-07 12:03:34

标签: javascript jquery

 <div class="comments_container clearfix">
 <hr />
<ul class="comment_struct">

      <li class="commenter_pic">
    <img src="images/im.jpg" alt="picture"/>
    <span class="button blue square likes">9 Likes</span>
      </li>
      <li class="comment_data">
           <ul class="post_info_options">
        <li class="commenter_name">Abc</li>
        <li class="comment"><p>hello how are you doing</p></li>
        <li class="time">
                        <span class="comment_time">12 June 2012<>
                       <span class="time_elapsed">7 hours ago</span>
          </li>
     <li class="options">
             <span><a href="#">Once More</a></span>
             <span><a href="#">Comment</a></span>
             <span><a href="#" class="comment_like">Like</a></span>
         </li>

     <li>

     </li>
 </ul>
</li>
<li class="delete_comment"><img src="images/Cancel.png" alt="delete_post"/></li>

   </ul>
   <br/>

    <ul class="comment_struct">

      <li class="commenter_pic">
    <img src="images/im.jpg" alt="picture"/>
    <span class="button blue square likes">9 Likes</span>
      </li>
      <li class="comment_data">
           <ul class="post_info_options">
        <li class="commenter_name">Abc</li>
        <li class="comment"><p>hello how are you doing</p></li>
        <li class="time">
                        <span class="comment_time">12 June 2012<>
                       <span class="time_elapsed">7 hours ago</span>
          </li>
     <li class="options">
             <span><a href="#">Once More</a></span>
             <span><a href="#">Comment</a></span>
             <span><a href="#" class="comment_like">Like</a></span>
         </li>

     <li>

     </li>
 </ul>
</li>
<li class="delete_comment"><img src="images/Cancel.png" alt="delete_post"/></li>

   </ul>
   <br/>



    <ul class="comment_struct">

      <li class="commenter_pic">
    <img src="images/im.jpg" alt="picture"/>
    <span class="button blue square likes">9 Likes</span>
      </li>
      <li class="comment_data">
           <ul class="post_info_options">
        <li class="commenter_name">Abc</li>
        <li class="comment"><p>hello how are you doing</p></li>
        <li class="time">
                        <span class="comment_time">12 June 2012<>
                       <span class="time_elapsed">7 hours ago</span>
          </li>
     <li class="options">
             <span><a href="#">Once More</a></span>
             <span><a href="#">Comment</a></span>
             <span><a href="#" class="comment_like">Like</a></span>
         </li>

     <li>

     </li>
 </ul>
</li>
<li class="delete_comment"><img src="images/Cancel.png" alt="delete_post"/></li>

   </ul>
   <br/>

我有如上所述的html结构..所以从$(&#39; .comments_container&#39;)开始,我如何使用class&#39; comment_time&#39;遍历到DOM的最后一个范围。帮助将不胜感激。

可以使用children()遍历。但我想知道有没有更简单的方法

1 个答案:

答案 0 :(得分:3)

您可以使用:last选择器:

var mySpan = $('.comments_container span.comment_time:last');