How to get the text value of the node

Node.prototype.nodeValue

// HTML 代码如下
// <div id="d1">hello world</div>
var div = document.getElementById('d1');
div.nodeValue // null
div.firstChild.nodeValue // "hello world"

The reason can not get nodeValue div, because for the div  element node , but only text nodes (text) / comment node (comment) / node attribute (attr) These three types of nodes have text values. 

The so-called value of the text, it is understood to be between the inner string quotes or label.

 

Easier to understand, only caveat is: The text between the tags is also a node type, ie: text nodes, so you can use .firstChild get.

 

 

  

Guess you like

Origin www.cnblogs.com/aisowe/p/11504259.html