【问题标题】:The JavaScript is not working in the webpageJavaScript 在网页中不起作用
【发布时间】:2023-01-15 00:45:57
【问题描述】:

But_1 正在工作,但它没有执行该功能。

它在浏览器控制台中给出错误cannot set property of null。 我试图通过函数 add() 中的 console.log('script is working') 检查 javascript,但它什么也没做

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let Count_el = document.getElementById('c1')
let count = 0
function add(){
count +=1
Count_el.innerHTML = count
}
</script>
<h1 id='heading'>Count</h1>
<h2 id="c1">0</h2>
<button id="but_1" onclick="add()">Increment</button>
<br>
<button id="but_2" onclick="">Save</button>
<br>
<p id="data">Previous Entries: </p>
</body>
</html>

【问题讨论】:

标签: javascript html css


【解决方案1】:

在你的 html 后面写上 javascript 代码。

<!DOCTYPE html>
  <html lang="en">
  <head>
    <link rel="stylesheet" href="index.css">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial- 
   scale=1.0">
    <title>Document</title>
  </head>
  <body>
    <h1 id='heading'>Count</h1>
    <h2 id="c1">0</h2>
    <button id="but_1" onclick="add()">Increment</button>
    <br>
    <button id="but_2" onclick="">Save</button>
    <br> 
    <p id="data">Previous Entries: </p>
    </body>
    <script>
      let Count_el = document.getElementById('c1')
      let count = 0
      function add(){
        count +=1
        Count_el.innerHTML = count
      }
  </script>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    相关资源
    最近更新 更多