在JavaScript中调用函数时出错

时间:2019-03-11 04:02:48

标签: javascript html

我正在尝试制作时间和日期时钟。尝试调用函数时遇到各种错误:

<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>

2 个答案:

答案 0 :(得分:0)

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>

答案 1 :(得分:0)

Onload不适用于EventEmitter标签,因此没有用。

<p>放在update()内将使其起作用。