当我使用.innerHTML函数查看html中的变量时出现未捕获的类型错误

问题描述 投票:-1回答:1

当我尝试在html页面中查看我的变量之一时,我什么也没看到。如果打开控制台,则会看到此错误

未捕获的TypeError:无法在my-java-script.js:17上将属性'innerHTML'设置为null。

我的脚本很简单。以下是代码:my-java-script.js:

var c = 3+4;
console.log(“The value of c is”, c);
var x=6 ;
var y=3;
var s = x-y ;
console.log(“Subtraction of” , x , “and” , y , “is”, s);
var m = x*y ;
console.log(“Multiplication of”, x , “and” , y , “is”, m);
var a = x+y ;
console.log(“Addition of”, x , “and” , y , “is”, a);
var d = x/y ;
console.log(“Division of”, x , “and” , y , “is”, d);

var ao = (x*y +10)/4;
console.log(ao);
var myVariable = “HELLO”;
document.getElementById(“demo”).innerHTML = myVariable;

my-java-webpage.html:

 <!DOCTYPE html>
<html>
<head>
    <title>Java Webpage </title>
    <script type="text/javascript" src = "my-java-script.js"></script>

</head>
<body>
    <h1> Java script</h1>

    <p id="demo"></p>
    </body>
</html>

以下是控制台和网页的图片: pic of the console and the webpage:

javascript html
1个回答
0
投票

这是因为“”未正确使用,还尝试使用刚关闭的正文中的脚本

      var c = 3+4;
      console.log("The value of c is", c);
      var x=6 ;
      var y=3;
      var s = x-y ;
      console.log("Subtraction of" , x , "and" , y , "is", s);
      var m = x*y ;
      console.log("Multiplication of", x , "and" , y , "is", m);
      var a = x+y ;
      console.log("Addition of", x , "and" , y , "is", a);
      var d = x/y ;
      console.log("Division of", x , "and" , y , "is", d);

      var ao = (x*y +10)/4;
      console.log(ao);
      var myVariable = "HELLO";
      document.getElementById("demo").innerHTML = myVariable;
<!DOCTYPE >
<html>
  <head>
    <title>Avengers Avenue</title>
  </head>
  <body>
    <h1>Java script</h1>

    <p id="demo"></p>
  <script type="text/javascript" src = "my-java-script.js"></script>
 </body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.