开发者

Jquery select only what is in this DIV?

HTML:

<div class="article">
<div class="article-content">
    <p><a href="javascript:;">Flygor</a> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam et est sit amet eros inter dum auctor vel semper quam. Vivamus non purus amet.</p>
    <div c开发者_运维技巧lass="wrap btn-actions">
        <div class="col"><a href="#" class="bt bt-medium" title="Retweet this" target="_blank">REPLY</a>
            <a href="javascript:;" class="bt bt-large retweet">RETWEET</a><a href="javascript:;" class="bt bt-icon">
        </div>
    </div>
</div>

I have this Javascript:

    $('.retweet').click(function() {
    var retweetText =  $(this).('.article-content p').text();
});

So i want the p tag in the DIV.article-content, but just in this one. I have about 10 article-contents on the page, and when the person clicks DIV.retweet i want to pull just the p tag in just that one DIV.


Your Javascript is really close, actually. You need to use .parents() because .article-content is multiple parents above .retweet, then go down to the child, p.

EDIT:

As @ShaneBlake points out, .closest() is better suited for this situation. .closest() only grabs the first .article-content it traversed, while .parents() would grab them all. Therefore, the code is now.

$('.retweet').click(function() {
   $(this).closest('.article-content').children('p').text();
});


try this :

var retweetText =  $(this).closest('.article-content').children('p').text();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜