English 中文(简体)
Javascript Clear DOM
原标题:Javascript Clear DOM

我完全想要用贾瓦特语清除OM。

I have tried a few things, like this:

document.getElementsByTagName("html")[0].innerHTML = "";
document.body.innerHTML = "";

令人感兴趣的是,将头盔清除在电子计算机中会产生错误(这一运行的“无效目标要素”),但将成功地清除 Chrome的头部。 然而,关闭了象EE公司那样的这个机构,但 Chrome没有保持沉默。

我也曾尝试过

document.childNodes.length = 0;

但是,这显然是单纯的财产,赢得了任何东西。

是否有很好的交叉浏览方式来清除OMM?

最佳回答

......

document.removeChild(document.documentElement);

jsFiddle。

请注意,浏览器将留下理论型态。

问题回答

提供。

This should work in HTML4:

var html = document.documentElement;
while(html.firstChild){
    html.removeChild(html.firstChild);
}

这里是有用的例子,不是完美的,而是可以加以利用。

/**
 * clear child nodes
 * @param {HTMLElement} node
 */
    clearChildren : function(node) {
        if (!node) {
            return;
        }
        if(node.innerHTML){
            node.innerHTML =   ;
            return;
        }
        while (node.childElementCount > 0) {
        var child = node.childNodes.item(0);
        if(isIE){
            child.onmousedown = null;
            child.onmouseover = null;
            child.onmouseout = null;
            child.onclick = null;
            child.oncontextmenu = null;
        }
        node.removeChild(child);
    }
    }

虽然没有交叉浏览器,但操作器有可操作的<代码>outer/2006/2财产。

document.documentElement.outerHTML = "";

可能非常简单

document.write();




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签