简体   繁体   English

如何从 jquery object 中删除 dom 元素

[英]How to delete dom element from jquery object

i want to delete a particular class from my object as my requirement is to delete that dom data before displaying content.我想从我的 object 中删除特定的 class 因为我的要求是在显示内容之前删除该 dom 数据。 I have written a sample code but not able to get why that is not working.我已经编写了一个示例代码,但无法理解为什么它不起作用。 I jquery's remove is also not working.我 jquery 的删除也不起作用。 Please help me to get it solve.请帮我解决它。 Thanks in advance提前致谢

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

You need to remove the content from the DOM directly.您需要直接从 DOM 中删除内容。

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

That will select the .p1 elements and remove them from the DOM这将.p1元素并将它们从 DOM 中删除

you can use remove function on javascript object.您可以在 javascript object 上使用删除 function。

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.现在使用jhtml。 demo演示

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

It seems that you're trying to duplicate a section, but without the .p1 elements.您似乎正在尝试复制一个部分,但没有.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.唯一的问题是您需要通过 go 并更新克隆中的 ID,以便它们不会在页面上重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM