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

标签 javascript innerhtml typeerror

我正在调用 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/

相关文章:

javascript - CasperJs 电子邮件在文本框中无效

javascript - 为什么 JS 的 BigInt 比正常数字慢 98%?

javascript - jQuery ("#"+id) 对比 document.getElementById (""+id)

javascript - 类型错误:字符串不是函数

node.js - 类型错误:无法读取未定义的属性 'getAppPath'

javascript - JQuery 更改标签中的文本

javascript - 调用 .length 时无法读取 null 的属性 'length'

javascript - JQuery 切换/html : changing the content in div

javascript - 无法设置 null 的属性 'innerHTML' 为什么我会收到此错误

当只有一个参数时,Python 会抛出多个参数错误