如何在父div中选择子div

时间:2013-06-27 12:57:37

标签: javascript jquery jquery-selectors

当我点击replyBtn时,我想选择类“postHandel”的html。 有多个“repost”div,我想要当前div的postHandel,我点击了replyBtn。

<div class="repost" data-id="52">
<div class="profileImg"></div>
<div class="postWrap">
    <div class="postUser">Tejas Kulkarni</div>
    <div class="postHandel">@tejas</div>
    <div class="postText"> </div> 
</div>
<a href="#" class="expandRP pull-right expandRP52" data-id="52"><i class="icon-chevron-down"></i></a>
<div class="clearfix"></div>

<div class="postOptions twtOptRP52">
    <ul>
        <li> <a class="btn btn-mini rtBtn" data-id="96"><i class="icon-retweet"></i> Repost</a></li>
        <li> <a class="btn btn-mini replyBtn" data-id="96"><i class="icon-reply"></i> Reply</a></li>
        <li> <a class="btn btn-mini favBtn" data-id="96"><i class="icon-star"></i> Fav</a></li>
        <li><div class="loadingPostsBtn hideme loadingBtn-96 hideme"><img src="http://linkzone.dev/assets/img/ajax-loader.gif"></div></li>
        <li><div class="loadingFailBtn hideme loadingFail-96 hideme"><img src="http://linkzone.dev/assets/img/ajax-fail.gif"></div></li>
    </ul>
</div>

我的jQuery代码是:

 $(document).on("click",".replyBtn",function(){

    var postHandel = $(this).closest(".postWrap").html('');
    var log = $(postHandel).closest(".postHandel").html();
    console.log(log);

});

2 个答案:

答案 0 :(得分:4)

试试这个:

var log = $(this).closest(".repost").find(".postHandel:first").html();

答案 1 :(得分:0)

$(this)                            // current element (.replybtn)
  .closest('.report')              // up to main wrapper (top-most grouping element)
  .find('.postWrap > .postHandel') // down to postHandel
  .html();                         // the end value

你可能甚至不需要.postWrap >鉴别器,但它确实确保它是 .postHandel你所追求的。