What is the equivalent of IE's removeNode

泄露秘密 提交于 2019-12-12 17:44:33

问题


IE's removeNode http://msdn.microsoft.com/en-us/library/ms536708(VS.85).aspx

helps me to decide whether I would like to remove childNodes or not.

I would like to know whether the same exists for Firefox, Opera, Chrome and Safari. If not, how can I achieve it?


回答1:


Didn't want just just copy code over, give this a read: http://www.sitepoint.com/forums//showthread.php?p=947385

Edit (but I, pst, have no shame -- code from above link ;-)

if ( window.Node )
Node.prototype.removeNode = function( removeChildren )
{
    var self = this;
    if ( Boolean( removeChildren ) )
    {
        return this.parentNode.removeChild( self );
    }
    else
    {
        var range = document.createRange();
        range.selectNodeContents( self );
        return this.parentNode.replaceChild( range.extractContents(), self );       
    }
}



回答2:


No, .removeNode() is not a standard method and it does not exist outside IE.

If you are looking for removeNode's functionality of promoting element's children up a level (an optional boolean argument), you have to do it manually.

However, with jQuery it is easy:

$(elToRemove).replaceWith($(elToRemove).children());


来源:https://stackoverflow.com/questions/4495789/what-is-the-equivalent-of-ies-removenode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!