使用javascript查找未标记的元素 [英] Finding untagged elements with javascript
本文介绍了使用javascript查找未标记的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< html>
< body>标签中的
< p>文本< / p>
任何标签之外的其他文字
< / body>
< / html>
如何获取untagged元素?甚至document.getElementsByTagName('*')不包含未标记的元素。
TIA
解决方案
只是在你的情况下,你可以简单地执行
document.getElementById('body')。lastChild
然而,在更一般的情况下,您需要循环遍历孩子并检查
a = document.getElementById('body')。lastChild
console.log(a.nodeType)// 3
console.log(a.nodeName)// #text
以下是 jsfiddle
Assuming I have some HTML with untagged content, e.g:
<html>
<body>
<p>text in a tag</p>
other text outside any tag
</body>
</html>
How can I get to the untagged element? Even document.getElementsByTagName('*') does not contain the untagged element.
TIA
解决方案
Just in your case you can do simply
document.getElementById('body').lastChild
However in a more general case you need to loop through children and check for
a = document.getElementById('body').lastChild
console.log(a.nodeType) // 3
console.log(a.nodeName) // #text
Here is example on jsfiddle
这篇关于使用javascript查找未标记的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文