我的代码几乎可以解释我将要得到的内容:
<div id=player>
<div class="button hand">►</div>
<div class=time>00:00/02:25</div>
<div class="timeline hand"><span class="now hand"></span></div>
</div>
<script>
var myPlayer=document.getElementById('player').firstChild;
var playerStatus=(myPlayer.innerText||myPlayer.textContent);
console.log(playerStatus);
</script>
我希望在控制台上得到 ascii 值 ►
。
这里需要做一些小调整
最佳答案
试试这个:
var myPlayer=document.getElementById('player').firstElementChild;
问题在于 #player
的第一个子节点本身就是一个文本节点。您正在寻找的是 #player
的第一个元素子元素。
小提示:IE8 不支持 firstElementChild
。
关于Javascript::firstChild 的 textContent/innerText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29834319/