1

如何删除 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>

我想删除这个parentstring-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

4 回答 4

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());
});
于 2011-08-27T16:14:11.780 回答
0

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

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

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

});

分离函数 jQuery

于 2011-08-27T18:15:01.173 回答
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);

});
于 2011-08-27T16:13:14.800 回答
0
$(".string-publish-needle").click(function () {
          $(this).remove();
        });

这有帮助吗?

于 2011-08-27T16:13:43.293 回答