code-snippets - SO Snippet 无法正确读取 childNodes
问题描述
在 Snippets 中运行以下命令将打印undefined
和 5,而不是“一”和 2:
const test = () => {
const select = document.getElementById("select");
const item0 = select.childNodes[0];
console.log(item0.innerText);
console.log(select.childNodes.length);
}
<select id="select" onchange="test()">
<option value='1'>One</option>
<option value='2'>Two</option>
</select>
似乎第一个<option>
在childNodes[1]
. 在网页中运行时,第一个childNodes
是第一个<option>
.
在普通网页中,childNodes.length
是<option>
元素的数量。
这是沙盒环境的错误还是已知限制?
解决方案
推荐阅读
- java - 如何将 Java 正则表达式匹配到结果字符串?
- security - U盘上软件的反盗版措施
- javascript - 未捕获的类型错误:window.Plivo 不是构造函数
- python - 我的 Python SQLite 代码容易发生 SQL 注入吗?
- android - 带有基线文件的 android lintoptions 不起作用
- wordpress - 更改固定链接后自定义帖子类型返回 404
- android - 不同主题的 PNG 会导致 InflateException
- postgresql - 当父记录可能存在或可能不存在时如何添加记录
- powershell - 如何使用 PowerShell 脚本删除文本/日志文件的内容,直到第一次出现关键字
- sql-server - 将前一个值设置为 NULL 用于 LEAD 功能