javascript - 在 JavaScript 中调用函数时出错
问题描述
我正在尝试制作时间和日期时钟。尝试调用函数时出现各种错误:
<html>
<head>
<title>Very Simple Clock By Ashton</title>
<script>
console.log("beginning creation of update function");
function update() {
document.getElementById('timeOutput').innerHTML = Date();
}
console.log("Successfully created update function");
console.log("Calling the update function")
update();
console.log("successfully called the update function");
</script>
</head>
<body>
<p onload="update()" id="timeOutput">ERROR | Could Not Retrieve Time from server</p>
</body>
解决方案
Body,Img,Iframe 标签只有onload
事件p
不支持。所以用onclick
事件而不是改变onload
仅加载p
console.log("beginning creation of update function");
function update() {
document.getElementById('timeOutput').innerHTML = Date();
}
<p onload="update()" id="timeOutput">ERROR | Could Not Retrieve Time from server</p>
改为点击
console.log("beginning creation of update function");
function update() {
document.getElementById('timeOutput').innerHTML = Date();
}
console.log("Successfully created update function");
console.log("Calling the update function")
update();
console.log("successfully called the update function");
<p onclick="update()" id="timeOutput">ERROR | Could Not Retrieve Time from server</p>
推荐阅读
- playframework - 无法使用带有 Macwire 的 DI 为子项目导入 Play Routes
- perl - Perl 打印以分隔文件
- php - 如果该循环在 laravel 中有内容,则从嵌套循环停止循环并删除特定循环集
- python - xlwings udf函数擦除excel上的下一个单元格
- r - Highcharts - 深入研究 R 中的多个系列
- r - 根据数据帧的间隔更改矩阵值
- angular - 具有多种资源的角度日历调度程序
- visual-studio-2017 - 无法在 VS 2017 中打开 VS 2015 .modelproj
- angular - 为什么 typescript 在我们定义类型时不隐式转换数据?
- c++ - 是否可以将 VBA 编程环境添加到我的 C++ 应用程序中?