倒计时脚本没有开始倒计时

时间:2013-01-02 00:30:19

标签: javascript jquery-plugins countdown

有人可以告诉我为什么倒计时功能不能正常工作(倒计时不是在带有I.D.“倒计时”的div中开始)。可能真的很简单,但Javascript不是我的强项。这是我正在使用的倒计时插件:http://keith-wood.name/countdown.html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Page Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="./css/web-style-second.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="chrome://mozbar/skin/css/injected.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.countdown.js"></script>
</head>
<body>
<a name="top"></a>
<div id="container">
    <div id="content">  

<div class="center">
    <center>
        <div></div>
    </center>
</div>

<script type="text/javascript"> 
$(function() {
    $('#countdown').countdown({
        until: '14min+20sec',
        format: 'MS',
        layout: '{mnn}min  ' + '{snn}sec  ',
        onExpiry: function() {
            $(".hidden").css("display", "block");
        }
    });
});
</script>

<div class="time_box"> 
    <h3 class="italic">When The Clock Hits Zero ... </h3> 
    <h4 class="hasCountdown" id="countdown">14min  20sec  </h4>  
</div>

<form name="signupFrm" id="signupFrm" action="#" method="post" class="hidden">
    <div>
        <div class="yellow_red">
            <h1></h1>
        </div>
        <div class="center">
            <a href="https://www.testing.com" class="btn_buy_now">
            Get Started</a>
        </div>
        <input name="submit" value="submit" type="hidden">
        <p class="mb_5">&nbsp;</p>
    </div>
</form>
<div class="fnote justify">
</div>


    </div>
    <div id="footer">
    </div>
</div>


</body>
</html>

3 个答案:

答案 0 :(得分:0)

您应该将$(function () ...)脚本放入HTML页面的<head>标记中。这样,一旦DOM完全加载,就会启动倒计时。

答案 1 :(得分:0)

我认为问题在于您缺少对“jQuery Countdown CSS”的引用。在usage instructions of the plugin中它说:

  

下载并包含jQuery Countdown CSS和JavaScript   页面的头部分。 @进口   “jquery.countdown.css”;

但是,如果您从class元素中移除<h4>元素且ID为“countdown”,那么您仍然可以在没有“jQuery Countdown CSS”的情况下使其工作,如下所示:

<div class="time_box"> 
    <h3 class="italic">When The Clock Hits Zero ... </h3> 
    <h4 id="countdown">14min  20sec  </h4>  
</div>

JSFiddle Example

答案 2 :(得分:0)

倒计时插件使用类hasCountdown来测试倒计时是否已经实例化,因此您应该使用不同的类名。

来自jquery.countdown.js:

/* Class name added to elements to indicate already configured with countdown.*/
markerClassName: 'hasCountdown'