[英]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.