未捕获的TypeError:无法读取null的属性'innerText'

时间:2019-06-04 10:21:11

标签: javascript

我不知道此错误,因为我只是检查了所有其他主题,但没有得到结果。

let demo = document.getElementById("#demo");
demo.innerText("yo");
<!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>Pract</title>
</head>
<body>
    <script src="javascript.js"></script>
    <p id="demo"></p>
</body>
</html>

1 个答案:

答案 0 :(得分:3)

//currently your id is demo

let demo = document.getElementById("demo");
demo.innerText="yo";

///if your id is  #demo use this
let demo2 = document.getElementById("#demo");
demo2.innerText="yolo";
<!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>Pract</title>
</head>
<body>
    <script src="javascript.js"></script>
    <p id="demo"></p>
    <p id="#demo"></p>
</body>
</html>

由于您的ID是demo,而不是#demo,因此您需要这样编写代码:

let demo = document.getElementById("demo");  // don't use #
demo.innerText = "yo";  // Also change innerText like this

要更改innerText属性,请查看docsinnerText不是方法。