简体   繁体   English

是否可以将childNodes.nodeValue和innerHTML一起使用?

[英]Is it possible to use childNodes.nodeValue and innerHTML together?

I have a js script that would getElementByTagname. 我有一个getElementByTagname的js脚本。 Then, i check it childNodes value.If i use this line of code: 然后,我检查它childNodes值。如果我使用这行代码:

var appname= name[i].childNodes[x].nodeValue;

If the nodeValue is Pang Shinichi ( Away from home ). 如果nodeValue是Pang Shinichi( 远离家乡 )。 The return of the script above became Pang Shinichi (). 上面脚本的回归变成了庞新一()。 HTML tag is not being read but instead it was consider as NULL. HTML标记未被读取,而是被视为NULL。 So, i was thinking if its possible to use childNodes.nodeValue and innerHTML at once. 所以,我在考虑是否可以立即使用childNodes.nodeValue和innerHTML。

Any idea on this? 有什么想法吗?

This is because your text is spread across several nodes. 这是因为您的文本分布在多个节点上。 For a quick solution, use textContent and innerText on the parent node instead: 要获得快速解决方案,请在父节点上使用textContentinnerText

var t = "textContent" in document.body ? "textContent" : "innerText",
    appname = name[i][t];

Internet Explorer 8 and lower will use innerText , whilst most other browsers will use textContent . Internet Explorer 8及更低版本将使用innerText ,而大多数其他浏览器将使用textContent There are slight differences between the behaviour of the two when it comes to white space, but for most use cases the results are identical. 当涉及到空白时,两者的行为之间存在细微差别,但对于大多数用例,结果是相同的。

Working demo: http://jsfiddle.net/7Qsjm/ 工作演示: http//jsfiddle.net/7Qsjm/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM