[英]trying to change inner html of h1 tag but js throwing an error
the javascript below is the line 13 and the h1 is my html tag
index.js:13 未捕获类型错误:无法在 index.js:13 处设置 null 的属性“innerHTML”
<h1 id="text">javascript practice</h1>
document.getElementById('text').innerHTML = 'hey there';
你必须确定,元素安装在 DOM 中
你可以这样使用
window.addEventListener('DOMContentLoaded', (event) => {
document.getElementById('text').innerHTML = 'hey there';
});
在此代码段中,您的代码在加载所有节点(元素)时运行
也许为 JavaScript 添加一个 function? 那将解决问题。 一个很好的例子是:
const h1 = document.getElementById('text');
h1.addEventListener('click', () => {
h1.innerHTML = <p>Hey There</p>
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.