javascript - 我得到 "cannot set property ' innerHTML' of null

标签 javascript html

我遇到了这个错误。我正在尝试输出 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/

相关文章:

javascript - 如何使用 node.js 和 redis Laravel 5 监听事件

javascript - 将日期值转换为 xslt 中的字符串日期格式

html 中的 JavaScript 错误

javascript - 如何使用 scrollTop 反转动画

javascript - webpack/HtmlWebpackPlugin错误: invalid configuration object

javascript - 无法在主干中使用 BaseView 调用未定义的方法 'extend'

javascript - 引用javascript中的错误消息?

jquery 加/减选择器

android - 如何在 Android 上自动播放 HTML5 mp4 视频?

javascript - 限制div中元素的数量