我需要知道文档元素是否是页面的ROOT节点。例如:
<html> <-- ROOT Node
<head></head>
<body>
<iframe>
<html>...</html> <-- other document
</iframe>
<iframe>
<html>...</html> <-- other document
</iframe>
</body>
</html>
在iframe 1或2中执行的Javascript应该知道他们的文档节点是否是根节点。
希望你能帮助我。
答案 0 :(得分:12)
您应该可以使用top
:
if (window.top.document === window.document) {
// we're in the outermost window
}
答案 1 :(得分:0)
我怀疑,鉴于an的内容是不同的文档,它们都会报告为根节点。您可能最好检查document.parent
是否为空。
答案 2 :(得分:0)
if (window == window.parent) {
alert("I'm not in a frame");
}
答案 3 :(得分:0)
在顶级文档中创建一个函数,返回它的rootNode,然后使用window.top参考从iframe文档中调用此函数:
在您的热门文档中:
function getRootNode()
{
//returns the rootNode
}
在您的iframe文档中:
var rootNode = window.top.document.getRootNode();
答案 4 :(得分:0)
试试这个:
if(currentnode.parentNode == null) { alert("is root node") }
//其中currentnode是您要选择的节点