我正在尝试用哈巴狗制作我的网页。
当我单击“mod user”按钮时,浏览器应显示用户名之一。
我的index.js如下。
block content
div(class='s1tablediv')
div(class='userListContainer')
div(class='userInfo' id='userInfo')
p(id='userID')
script.
function fuserInfo(arg) {
$('.userInfo').text('');
var userIdElem = document.getElementById('userID');
userIdElem.innerHTML = arg.innerHTML;
}
但是,当我单击按钮时,我收到“TypeError: userIdElem is null”。
如果像这样更改代码,
div(class='userInfo' id='userInfo')
p(id='userID') // unindent one level.
效果很好。
请告诉我原因。
最佳答案
脚本何时开始评估
$('.userInfo').text('');
删除具有 ID UserID 的段落
然后您尝试使用 document.getElementById('userId') 选择 ID
因为 DOM 中不再有它,所以返回 Null
关于javascript - '在 javascript 中调用 getElementById 时出现 TypeError : element is null'.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61224469/