简体   繁体   English

在 JavaScript 中调用函数时出错

[英]Errors while calling a function in JavaScript

I am trying to make a time and date clock.我正在尝试制作时间和日期时钟。 I am getting all sorts of errors when trying to call a function:尝试调用函数时出现各种错误:

<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 tags only have onload event p not supported.So Change with onclick event instead of onload Body,Img,Iframe 标签只有onload事件p不支持。所以用onclick事件而不是onload

Only onload of p仅加载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>

Change to click更改为点击

 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>

Onload does not work for <p> tags, so it is useless. Onload 对<p>标签不起作用,所以它没用。

Enclosing update() inside window.onload() would allow it to work.update()window.onload()中将允许它工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM