Javascript::firstChild 的 textContent/innerText

标签 javascript html console innertext

我的代码几乎可以解释我将要得到的内容:

<div id=player>
    <div class="button hand">&#9658;</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/

相关文章:

javascript - 动态添加元素上的 mousewheel 事件

javascript - 我想通过从 javascript 函数传递参数来调用 scriplet 中的方法,该函数在 html 中单击按钮时调用

linux - 如何让终端建议自定义命令的选项?

javascript - 如何 XHR 远程服务器?

javascript - 指定在任何 Jest 设置发生之前运行的代码

java - 查找复选框是否在 servlet 中被选中

javascript - 如何阻止 .focusout() 多次触发?

javascript - 如何 : Make a Div a link, 而不是其中的图像

c++ - 我如何简单地读取控制台输出

javascript - 数组显示其长度为 0 但控制台显示否则