[英]Cannot access innerHTML of span tag
I have this piece of code: 我有这段代码:
var size = iframe.contentWindow.document.getElementsByClassName('checked').getElementsByTagName('span')[0].innerHTML;
For some reason every time I run it I get this message: 由于某种原因,每次我运行它时,都会收到以下消息:
Uncaught TypeError: Object [object NodeList] has no method 'getElementsByTagName' 未捕获的TypeError:对象[object NodeList]没有方法'getElementsByTagName'
Even though all my other functions with getElementsByTagName()
work fine. 即使我使用getElementsByTagName()
所有其他功能都可以正常工作。 What could be the reason for this error? 此错误的原因可能是什么?
Thanks 谢谢
getElementsByClassName
returns a set of elements (ie NodeList
), not a single element: getElementsByClassName
返回一组元素(即NodeList
),而不是单个元素:
iframe
.contentWindow
.document
.getElementsByClassName('checked')[0] // <--- [0] (or whatever index)
.getElementsByTagName('span')[0]
.innerHTML;
Besides you can replace it with querySelector
: 此外,您可以将其替换为querySelector
:
iframe
.contentWindow
.document
.querySelector(".checked span")
.innerHTML;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.