我遇到了这个错误。我正在尝试输出 html 文档中数组中存在的名称,但我做不到。我尝试将脚本标记放在正文下方,但仍然无法正常工作。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1> Callback Function</h1>
<ul class"people"> </ul>
<script type="text/javascript" src="callme.js">
</script>
</body>
</html>
// get a reference to the 'ul'
const ul = document.querySelector('.people');
//I am trying to read the people on to the html code
const people = ['mario', 'luigi', 'ryu', 'shaun', 'chun-li'];
let html = ` `;
people.forEach(function(person){
html += `<li> style="color: purple">${person}</li>`;
});
console.log("hellow");
ul.innerHTML = html;
最佳答案
您的 HTML 中有错误。 试试这个:
<ul class="people"> </ul>
class 和“people”之间少了一个=号。
关于javascript - 我得到 "cannot set property ' innerHTML' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58860195/