English 中文(简体)
Javacast DOM 删除要素
原标题:JavaScript DOM remove element

如果存在多功能元素,如果确实存在,那么我会尝试测试,如果确实存在,就会删除,如果确实存在这种成分的话。

var duskdawnkey = localStorage["duskdawnkey"];
var iframe = document.createElement("iframe");
var whereto = document.getElementById("debug");
var frameid = document.getElementById("injected_frame");
iframe.setAttribute("id", "injected_frame");
iframe.setAttribute("src",  http://google.com );
iframe.setAttribute("width", "100%");
iframe.setAttribute("height", "400");

if (frameid) // check and see if iframe is already on page
{ //yes? Remove iframe
    iframe.removeChild(frameid.childNodes[0]);
} else // no? Inject iframe
{
    whereto.appendChild(iframe);
    // add the newly created element and it s content into the DOM
    my_div = document.getElementById("debug");
    document.body.insertBefore(iframe, my_div);
}

如果存在工作,则检查内容,但删除内容。 基本上,所有这部法典都通过点击纽子将一字块注入网页。 我所想的是,如果已经有一机删除。 但出于某种原因,我失败了。

最佳回答

https://developer.mozilla.org/en-US/docs/Web/API/Node.removeChild”rel=“noreferer”>removeChild 父母应当援引:

parent.removeChild(child);

在你的例子中,你应当做这样的事情:

if (frameid) {
    frameid.parentNode.removeChild(frameid);
}
问题回答

在大多数浏览器中,从OMA中删除一个内容的简明方式比在括号上打上.removeChild(element),即只打上.removelement()。 在适当时候,这很可能成为从人力部删除一个要素的标准和辅助方法。

https://developer.mozilla.org/en-US/docs/Web/API/ChildNode.remove” rel=“noreferer”>.remove( 在2011年,在DOM生活标准(commit)中添加了这种方法。 它没有得到任何版本的因特网探索者的支持。

如果你想要支持老的浏览器,那么你就得ll。 这似乎令人不快,因为似乎没有人把所有目的的DOMhim都包含这些方法,而且因为我们不仅将这种方法添加到单一的原型上;它采用了一种“的方法,该编码只是由光谱确定的一个接口,而且可供Javales查阅,因此我们可以在其原型上添加任何内容。 因此,我们需要找到从<代码>ChildNode继承的所有原型,这些原型实际上在浏览器中界定,并添加<代码>.remove。

Here s the shim I came up with, which I ve confirmed works in IE 8.

(function () {
    var typesToPatch = [ DocumentType ,  Element ,  CharacterData ],
        remove = function () {
            // The check here seems pointless, since we re not adding this
            // method to the prototypes of any any elements that CAN be the
            // root of the DOM. However, it s required by spec (see point 1 of
            // https://dom.spec.whatwg.org/#dom-childnode-remove) and would
            // theoretically make a difference if somebody .apply()ed this
            // method to the DOM s root node, so let s roll with it.
            if (this.parentNode != null) {
                this.parentNode.removeChild(this);
            }
        };

    for (var i=0; i<typesToPatch.length; i++) {
        var type = typesToPatch[i];
        if (window[type] && !window[type].prototype.remove) {
            window[type].prototype.remove = remove;
        }
    }
})();

以来,这在IE 7或以下赢得了t工作。 DOM 原型在8之前是可能的。 不过,我的数字是,在2015年ver,大多数人都需要对这种事情进行护理。

Once you ve included them shim, you ll be able to remove a DOM element element from the DOM by simply calling

element.remove();

我只字不提发表评论,因此,必须作出另一种回答。

When you unlink a node using removeChild() or by setting the innerHTML property on the parent, you also need to make sure that there is nothing else referencing it otherwise it won t actually be destroyed and will lead to a memory leak. There are lots of ways in which you could have taken a reference to the node before calling removeChild() and you have to make sure those references that have not gone out of scope are explicitly removed.

Doug Crockford writtens here 该活动的处理者在《儿童法》中被称作是通函参考资料的原因,并建议在要求删除之前将其明确删除。

function purge(d) {
    var a = d.attributes, i, l, n;
    if (a) {
        for (i = a.length - 1; i >= 0; i -= 1) {
            n = a[i].name;
            if (typeof d[n] ===  function ) {
                d[n] = null;
            }
        }
    }
    a = d.childNodes;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            purge(d.childNodes[i]);
        }
    }
}

即使你采取许多防范措施,你仍然能够像Jens-Ingo Farley所描述的那样,在IE中获取记忆,here

最后,没有把 Javat delete作为答案。 许多人似乎建议,但获得工作经验:Here,这是Kangax在理解delete方面的一大参考。

Node.removeChild ()做你的工作,只是用这样的东西:

var leftSection = document.getElementById( left-section );
leftSection.parentNode.removeChild(leftSection);

在OM4中,采用了移除方法,但根据W3C,浏览器支持不足:

The method node.remove() is implemented in the DOM 4 specification. But because of poor browser support, you should not use it.

But you can use remove method if you using jQuery...

$( #left-section ).remove(); //using remove method in jQuery

同样,在新的框架中,如你可以利用各种条件去除一个要素,例如<条码>*纳克If,在安热和React,提出不同看法,取决于条件......





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签