从html()中删除元素?

时间:2011-08-27 16:09:31

标签: jquery html click parents

如何删除html()中的某些元素?

例如,我有这个HTML链接,

<ul>
    <li><a href="#" class="string-comment">Comment</a></li>
    <li><a href="#" class="string-delete-needle">Delete</a></li>
    <li><a href="#" class="string-publish-needle">Publish</a></li>
</ul>

当您点击string-publish-needle的类时,我会将html存储在变量中,

var html_parents = object_parents.html();

<li><a href="#" class="string-comment">Comment</a></li>
<li><a href="#" class="string-delete-needle">Delete</a></li>
<li><a href="#" class="string-publish-needle">Publish</a></li>

我要删除parent string-publish-needle的这个,

<li><a href="#" class="string-publish-needle">Publish</a></li>

所以我只能在下面有一个新的html内容,

<li><a href="#" class="string-comment">Comment</a></li>
<li><a href="#" class="string-delete-needle">Delete</a></li>

我的代码,但当然不起作用!

$('.string-publish-needle').click(function(e){

    var object = $(this);
    var object_path = object.attr('href');
    var object_parent = object.parent('li');
    var object_parents = object.parents('ul');
    var html_parents = object_parents.html();
    var html_remain = object_parents.html().remove(object_parent);

    alert(html_remain);

});

4 个答案:

答案 0 :(得分:1)

$('.string-publish-needle').click(function(e){
    var copy = $($('<ul/>').append($(this).parents('ul').html()));
    copy.find('.string-publish-needle').parent('li').remove();
    alert(copy.html());
});

答案 1 :(得分:0)

$('.string-publish-needle').click(function(e){

    var object = $(this);
    var object_path = object.attr('href');
    var object_parent = object.parent('li');
    var object_parents = object.parents('ul');
    var html_parents = object_parents.html();
    object_parent.remove();
    var html_remain = object_parents.html();

    alert(html_remain);

});

答案 2 :(得分:0)

$(".string-publish-needle").click(function () {
          $(this).remove();
        });

这有帮助吗?

答案 3 :(得分:0)

我不知道我是否理解得很好...... 但 你可以做到

$('.string-publish-needle').click(function(e) {

    var x = $(this).detach();

});

detach function jQuery