JS不起作用,即使在使用window.load之后也是如此

JS doesnt work, even after using window.load

本文关键字:之后 load 不起作用 JS window      更新时间:2023-09-26

我的问题与以下内容相同:

非常简单的javascript根本不起作用

但就我而言,答案无济于事。

在 JS 小提琴中它确实有效,但是当我复制 JS 小提琴结果帧的源代码时,它不再工作(在我的服务器/计算机上)。

我的代码(简体):

<!DOCTYPE html>
<html>
<head>
    <title>Stack Overflow</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
    <p id="countdown2"></p>
    <script>
    function countdown(element, minutes, seconds) {
    var time = minutes*60 + seconds;
    var interval = setInterval(function() {
        var el = document.getElementById("countdown2");
        if(time == 0) {
            el.innerHTML = "countdown's over!";    
            clearInterval(interval);
            return;
        }
        var minutes = Number.floor( time / 60 );
        if (minutes < 10) minutes = "0" + minutes;
        var seconds = time % 60;
        if (seconds < 10) seconds = "0" + seconds; 
        var text = minutes + ':' + seconds;
        el.innerHTML = text;
        time--;
    }, 1000);
    }
    countdown("countdown2", 9, 23);
    </script>
</body>
</html>

在这个JS小提琴中,它可以工作。http://jsfiddle.net/Apnu2/374/

我的简化在线页面:

http://biefstuk.org/mc/faal.html

我已经为此工作了一个多小时了...

你的小提琴加载了MooTools,但你的页面没有。

改变:

var minutes = Number.floor( time / 60 );

var minutes = Math.floor( time / 60 );

并使用普通 JS。

js小提琴示例

Number没有方法floor。请改用Math

Math.floor( time / 60 );

你的jsFiddle加载MooTools,它使用floor方法扩展本机Number对象。

您的代码

没有,因此您的代码在尝试调用 Number.floor 时会遇到错误。请改用 Math.floor。

好吧,并在定义后调用倒计时。