jscript - 很难在 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>
解决方案
这个 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>
推荐阅读
- android - Android将地图中的多边形保存为所有设备大小相同的位图
- python-3.x - 使用 Python 3 进行抓取
- azure - Get-AzDataLakeStoreItem 返回 GETFILESTATUS failed with Unknown Error for valid items
- git - 正确设置文件夹中的 .gitignore 文件
- c# - Visual Studio Mac Intellisense 在按下回车键时生成新行而不是完成文本
- c - 赢得猜字游戏时无法终止 for 循环
- laravel - 在 Laravel 中获取空数据
- aws-lambda - aws sam 发布/部署流程
- swift - 如果 Swift 协议是用类型约束定义的,为什么不能直接访问该类型的属性/方法?
- excel - 通过 FTP 发布 Excel 加载项