Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript body.childNodes的nodeValue在哪里?_Javascript_Dom - Fatal编程技术网

Javascript body.childNodes的nodeValue在哪里?

Javascript body.childNodes的nodeValue在哪里?,javascript,dom,Javascript,Dom,我运行了这段Javascript代码,获得了body.childNode的nodeType和nodeName,没有问题。但是,nodeValue无法显示 <!DOCTYPE html> <html> <body> <p>Click the button to get the node types of the body element's child nodes.</p> <button onclick="myFunction(

我运行了这段Javascript代码,获得了body.childNode的nodeType和nodeName,没有问题。但是,nodeValue无法显示

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get the node types of the body element's child nodes.</p>
<button onclick="myFunction()">Try it</button>
<p><strong>Note:</strong> Whitespace inside elements is considered as text, and text is considered as nodes.</p>
<!-- My personal comment goes here..  -->
<div><strong>Note:</strong> Comments in the document are considered as comment nodes.</div>
<p id="demo"></p>

<script>
function myFunction() {
    var c = document.body.childNodes;
    var txt = "";
    var i;
    for (i = 0; i < c.length; i++) {
        txt = txt + "notdType: "+ c[i].nodeType + "    NodeName: "+c[i].nodeName+"    NodeValue: "+c[i].nodeValue +"<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>

单击该按钮以获取主体元素的子节点的节点类型

试试看 注意:元素内部的空白被视为文本,文本被视为节点

注意:文档中的注释被视为注释节点。

函数myFunction(){ var c=document.body.childNodes; var txt=“”; var i; 对于(i=0;i”; } document.getElementById(“demo”).innerHTML=txt; }
元素节点没有节点值。相反,它们有自己的子节点。

想想看:为什么代码要遍历document.body.childNodes而不是查找其节点值?可能是因为document.body也没有nodeValue吗?然后尝试将并没有nodeValue的主体和并没有nodeValue的节点联系起来。然后,看看你们是否能找出为什么其他节点并没有nodeValue。这似乎是成功的。如果元素节点有它们的子节点,我可以在元素节点之后添加firstChild并获得nodeValue吗?文本节点和元素节点之间有什么区别?nodeValue是否仅应用于文本节点?