首页 > 解决方案 > 在 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>

标签: javascripthtml

解决方案


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>


推荐阅读