This question already has answers here:
How do I change the text of a span element in JavaScript
                                
                                    (12个答案)
                                
                        
                                7个月前关闭。
            
                    
我不知道此错误,因为我只是检查了所有其他主题,但未得到结果。



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>

最佳答案

//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不是方法。

关于javascript - 未捕获的TypeError:无法读取null的属性'innerText',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56441944/

10-12 06:35