首页 > 解决方案 > 很难在 jscript 上编写简单的代码

问题描述

我无缘无故地使用这个简单的 jscript。此代码应在单击后显示时间。但我收到以下错误。我究竟做错了什么

{
  "message": "Uncaught TypeError: Cannot set properties of null (setting 'innerHTML')",
  "filename": "https://stacksnippets.net/js",
  "lineno": 30,
  "colno": 43
}

function item(name)
{

 var d = new Date();
document.getElementById("but1").innerHTML = d;

}
<!DOCTYPE html>
<html>
<body>
    <script src="C:\Users\kalyanasundar.s\OneDrive - HCL Technologies Ltd\Desktop\proj\animation\index.Js"></script>

<h2>JavaScript Statements</h2>

<p id="demo">You cannot break a code line with a \ backslash.</p>
<button onclick="item(name)">Try it</button>



</body>
</html>

标签: jscript

解决方案


这个 id (#but1) 不存在,所以 innerHtml 不起作用,尝试更改它。

function item(name) {

  var d = new Date();
  document.getElementById("demo").innerHTML = d;

}
<!DOCTYPE html>
<html>

<body>
  <script src="C:\Users\kalyanasundar.s\OneDrive - HCL Technologies Ltd\Desktop\proj\animation\index.Js"></script>

  <h2>JavaScript Statements</h2>

  <p id="demo">You cannot break a code line with a \ backslash.</p>
  <button onclick="item(name)">Try it</button>



</body>

</html>


推荐阅读