【发布时间】:2019-11-16 16:07:23
【问题描述】:
我尝试获取一个元素并对其进行修改。
它看起来像在开发者控制台中;
document.querySelectorAll('[role="presentation"]').forEach(function(el) {
console.log('test');
console.log('innerHTML: ' + el.innerHTML);
});
<div id="toolBarId">
<table>
<tbody>
<tr role="presentation">someElements1</tr>
<tr>someElements2</tr>
</tbody>
</table>
</div>
如果我写;
document.getElementById('toolBarId').innerHTML;
打印出来;
someElements2
我试过了;
document.querySelectorAll('[role="presentation"]').forEach(function (el){
console.log('test');
// console.log('innerHTML: ' + el.innerHTML);
});
它什么也不打印。
编辑: 我在基本 html 中的“/body”之前添加了这个。没用。
<script type="text/javascript">
document.querySelectorAll('[role="presentation"]').forEach(function (el){
console.log('test');
// console.log('innerHTML: ' + el.innerHTML);
});
</script>
</body>
编辑2:
if(document.readyState === "complete") {
testPresentation() ;
// seems never called
}
else {
window.addEventListener("onload", function ()
{testPresentation() ;}, false);
// seems never called
//or
document.addEventListener("DOMContentLoaded", function () {testPresentation() ;}, false);
// called but selector cannot find first row of the table still
}
function testPresentation() {
console.log('testPresentation called');
document.querySelectorAll('[role="presentation"]').forEach(function (el){
console.log('test');
// console.log('innerHTML: ' + el.innerHTML);
});
}
【问题讨论】:
-
那会是什么问题呢?也许生成表的 js 代码在我的查询选择器之后运行?但是我也不会得到第二排。
-
从第一条语句的打印结果来看,当该语句运行时,第一行似乎不存在(具有
role属性的行)。看起来#toolBarIddiv 不包含表格,它只有以下文本:someElements2 -
我可以安排一些优先级查询选择器将是代码最终运行吗?谢谢
-
是的,在生成表格的函数完成后调用。
标签: javascript html