从 html() 中删除一个元素?

发布于 2024-12-01 14:43:44 字数 1560 浏览 1 评论 0原文

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

我想删除这个string-publish-needle 的 >parent

<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);

});

How can I remove certain element inside html()?

For instance, I have this html links,

<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>

When you click on the class of string-publish-needle, I will store the html in a variable,

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>

and I want to remove this one which is the parent of string-publish-needle,

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

so I can have a new html content below only,

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

My code but does not work of course!

$('.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);

});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

愛上了 2024-12-08 14:43:44
$('.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());
});
$('.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());
});
森林散布 2024-12-08 14:43:44
$('.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);

});
$('.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);

});
一生独一 2024-12-08 14:43:44
$(".string-publish-needle").click(function () {
          $(this).remove();
        });

这有帮助吗?

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

Does this help ?

莳間冲淡了誓言ζ 2024-12-08 14:43:44

不知道我理解得好不好...

你可以做

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

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

});

分离函数 jQuery

I don't know if I understood well...
but
you can do

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

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

});

detach function jQuery

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文