gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot call method 'innerHTML' of null even when I place my script code inside tag

转载 作者:行者123 更新时间:2023-11-29 21:38:33 25 4
gpt4 key购买 nike

我在 JavaScript 中调用一个函数,如图所示

var count = 0;
function resetGroupsSelector(groupId){
//alert(groupId);
console.log("search_report_form:"+groupId)
//alert("search_report_form:"+groupId)
var id = "search_report_form:"+groupId;
document.getElementById("id").innerHTML("HI");
}

但是我得到了

Uncaught TypeError: Cannot call method 'innerHTML' of null

错误。我不知道出了什么问题。

最佳答案

简单语法错误:

document.getElementById("id").innerHTML("HI");

应该改成这样:

document.getElementById(id).innerHTML = "HI";
^^ ^^^^^^^

不要忘记从 id 中删除引号,因为它是一个变量,所以您要确保传递的是变量而不是 < em>文字字符串 "id"

关于javascript - 未捕获的类型错误 : Cannot call method 'innerHTML' of null even when I place my script code inside <body> tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34035058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com