jQuery通过单击div中的某些内容来选择div html(). [英] jQuery selection div html() by click something in div.

查看:84
本文介绍了jQuery通过单击div中的某些内容来选择div html().的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,如何通过单击删除"来选择发布" div html().
如果我写$(''post'').html()选择仅对第一个"post"起作用,但是我需要选择第二个或第三个.

Hi, how can I select "post" div html() by click "remove".
If I write $(''post'').html() select work only for first "post", but I need to select for example second or third.

//html
<div class="posts">
    <div class="post">
       Some text for first div
       <a class="remove">remove</a>
       <hr>
    </div>

    <div class="post">
       Some text for second div
       <a class="remove">remove</a>
       <hr>
    </div>

    <div class="post">
       Some text for third div
       <a class="remove">remove</a>
       <hr>
    </div>
</div>

// jQuery
$('.remove').live('click', function() {
    var postHTML = $(this).html(); // select only "a" element
});

推荐答案

(``post'').html()选择仅适用于第一个"post",但我需要选择例如第二或第三.

(''post'').html() select work only for first "post", but I need to select for example second or third.

//html
<div class="posts">
    <div class="post">
       Some text for first div
       <a class="remove">remove</a>
       <hr>
    </div>

    <div class="post">
       Some text for second div
       <a class="remove">remove</a>
       <hr>
    </div>

    <div class="post">
       Some text for third div
       <a class="remove">remove</a>
       <hr>
    </div>
</div>

// jQuery


('.remove').live('click',function(){ var postHTML =
('.remove').live('click', function() { var postHTML =


(this).html(); //仅选择"a"元素 });
(this).html(); // select only "a" element });


这篇关于jQuery通过单击div中的某些内容来选择div html().的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆