我正在尝试获取 span 元素的文本。我尝试了两种方法querySelectorAll
,getElementByClassName
但都给了我错误“未捕获的类型错误:对象#没有方法'getElementsByTagName'”我该如何获取跨度元素的文本?
javascript
var interstitial;
// 1st attempt
interstitial = document.querySelectorAll('div.top_bg').getElementsByTagName("span");
// 2nd attempt
interstitial = document.getElementByClassName('top_bg')[0].getElementsByTagName("span");
if (interstitial) {
console.log(interstitial[0].firstChild.innerHTML);
}
html
<div class="top_bg">
<span style="font-size:14px;line-height:30px">The text I am trying to get.</span>
</div>