目录结构:
contents structure
[+]
Note.noteValue 属性返回或设置当前属性的值。
语法
value = node.nodeValue;
如果有值的话,value则是一个包含当前节点值的字符串,如果没有则是null。
注意
对于document文档自身来说,nodeValue返回null。对于元素节点,nodeValue返回null。对于文本节点、注释和CDATA部分来说,noteValue返回其节点的内容。 对于节点属性来说,属性的值将会被返回。
下面的表列出了不同元素的返回值,
| Attr | value of attribute |
|---|---|
| CDATASection | content of the CDATA Section |
| Comment | content of the comment |
| Document | null |
| DocumentFragment | null |
| DocumentType | null |
| Element | null |
| NamedNodeMap | null |
| EntityReference | null |
| Notation | null |
| ProcessingInstruction | entire content excluding the target |
| Text | content of the text node |
详述
实例
<!DOCTYPE html>
<html>
<head>
<title>noteValue.html</title>
<meta name="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<div id="emp"></div>
<div id="full">I hava contents</div>
<script>
//返回null
var val1=document.nodeValue;
//返回null,因为val2代表的是一个元素节点的noteValue。
var val2=document.getElementById("emp").nodeValue;
//报错,因为val3代表的节点无子节点
var val3=document.getElementById("emp").firstChild.nodeValue;
//返回null,因为val4代表的是一个元素节点的noteValue.
var val4=document.getElementById("full").nodeValue;
//返回一个"I hava contents",因为val5代表的是一个文本节点的子节点
var val5=document.getElementById("full").firstChild.nodeValue;
</script>
</body>
</html>
参考文章
本文为博主原创翻译,如需转载请注明出处。
本文深入解析了 DOM 中 NodeValue 属性的功能与使用方法,包括其语法、注意事项及不同节点类型的返回值特点,并通过实例代码展示了如何在 HTML 文档中获取节点值。
1573

被折叠的 条评论
为什么被折叠?



