Javascript 如何从jquery对象中删除dom元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6695594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:51:17  来源:igfitidea点击:

How to delete dom element from jquery object

javascriptjqueryjquery-selectors

提问by Rahul

i want to delete a particular class from my object as my requirement is to delete that dom data before displaying content. I have written a sample code but not able to get why that is not working. I jquery's remove is also not working. Please help me to get it solve. Thanks in advance

我想从我的对象中删除一个特定的类,因为我的要求是在显示内容之前删除该 dom 数据。我写了一个示例代码,但无法理解为什么它不起作用。我 jquery 的删除也不起作用。请帮我解决。提前致谢

<html>
<head>
<title>test</title>
<script type="text/javascript" src="jquery-1.5.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {

    // complete html
    var test;
    test = $('#issue_detail_first_row').html();

    var x = $(test).find('#issue_detail_nav').not('.p1');

    $('#sett').html(x);
 });

</script>
 </head>
 <body>
 <div id="issueDetailContainer">
        <div  id="issue_detail_first_row">
            <div>
                <div id="issue_detail_nav">
                    <div>test</div>
                    <div id="gett">
                        <div class="p1">
                            this content need to be deleted 1
                        </div>
                    </div>

                    <div class="p1">
                        this content need to be deleted 2
                    </div>

                </div>
            </div>                
        </div>
        <br/><br/><br/><br/>
<div id="sett">
</div>

回答by Bobby Borszich

You need to remove the content from the DOM directly.

您需要直接从 DOM 中删除内容。

$("#issue_detail_first_row .p1").remove();

That will select the .p1elements and remove them from the DOM

这将选择.p1元素并将它们从 DOM 中删除

回答by Vivek Goel

you can use remove function on javascript object.

您可以在 javascript 对象上使用 remove 函数。

If you want to preprocess it before displaying.

如果要在显示前对其进行预处理。

example

例子

var a =$("#issue_detail_first_row").html();
var jhtml =$(a)[0];   
$(jhtml).find('.p1').remove();
alert($(jhtml).html());

now use jhtml . demo

现在使用 jhtml 。演示

http://jsfiddle.net/WXPab/14/

http://jsfiddle.net/WXPab/14/

回答by user113716

It seems that you're trying to duplicatea section, but without the .p1elements.

您似乎正在尝试复制一个部分,但没有.p1元素。

You could use the clone()[docs]method to clone the section, the remove()[docs]method to remove what you don't want, and then insert its HTML.

您可以使用clone()[docs]方法克隆该部分,使用remove()[docs]方法删除您不想要的内容,然后插入其 HTML。

$(document).ready(function() {

    var test = $('#issue_detail_first_row').clone();  // clone it

    test.find('.p1').remove();  // find and remove the class p1 elements

    $('#sett').html( test.html() );  // insert the new content
});

Working example:http://jsfiddle.net/YDZ9U/1/

工作示例:http : //jsfiddle.net/YDZ9U/1/

Only thing is that you'll need to go through and update the IDs in the clone, so that they're not duplicated on the page.

唯一的问题是您需要检查并更新克隆中的 ID,以便它们不会在页面上重复。