html does not equal document.childNode[0]
html does not equal document.childNode[0]
下面是我的代码。我不知道为什么var html不等于document.childNodes[0]。它不断返回假。有什么帮助吗?
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
var html = document.documentElement;
alert(html === document.childNodes[0]);
alert(html === document.firstChild);
</script>
</html>
这是因为您的第一个节点是DOCTYPE
声明。
HTML 元素document.childNodes[1]
在文档中。
如果你想要的是任何HTML文档中的html
元素,这也是document.documentElement
哪个更可靠(参见MDN)。
试试这个:
alert(html === document.firstElementChild);
相关文章:
- 正在寻找比$(document).ready慢的$(window).load的替代方案
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 如何用更合适的内容替换document.write
- document.styleSheets不返回任何内容
- jQuery document.ready not working
- $(document).height()在刷新时随机化值(Safari 5.1.10)
- reactjs this.refs vs document.getElementById
- 如何通过adf中的document.getElementById获取inputText字段值
- window.opener.document在ie中不起作用
- contentWindow.document.body is null
- document.applet.method在Mounatin Lion上抛出safari 6+JDK7异常
- document.getElementById(“st”).click();不起作用
- $(document).height和$(window).heaght都返回相同的值-使用的是正确的doctype
- jQuery document.ready停止代码
- document.getElementById并使用id名称
- 使用jQuery 1.8.1准备好多个$(document).是否有任何开销
- document.getElementById在js中不起作用
- document.getElementById.style.backgroundImage not working
- 如何使用document.getElementsByClassName()访问
- html does not equal document.childNode[0]