多个jQuery倒计时实例无法正常工作

时间:2014-01-15 03:15:23

标签: javascript jquery wordpress countdown

我正在使用jQuery倒计时插件(可以在这里下载:http://keith-wood.name/countdown.html)。

我已经实现了它并且当页面上有单个实例(倒计时)时它可以工作,但是当有超过1个实例时,它不起作用。

我正在使用的代码:

<div id="defaultCountdown"></div>

<script type="text/javascript">
jQuery(function () {
    var austDay = new Date();
    // Set some date in the past. In this case, it's always been since Jan 1
    var offerEndDateTS =  <?php echo get_post_meta($post->ID, '_sale_price_dates_to', true); ?>;
    var offerEndDate = new Date(offerEndDateTS * 1000);
    austDay = new Date(offerEndDateTS * 1000);
    jQuery('#defaultCountdown').countdown({until: austDay});
    jQuery('#year').text(austDay.getFullYear());

});
</script>

有关如何使同一页面上的所有计数器都能正常工作的任何想法?谢谢你的时间

2 个答案:

答案 0 :(得分:2)

您是否尝试过阅读reference documentation或查看演示?

似乎对我有用:

HTML

<div id="defaultCountdown"></div>
<div id="defaultCountdown2"></div>

JavaScript的:

$('#defaultCountdown').countdown({until: new Date(2014, 1, 1)});
$('#defaultCountdown2').countdown({until: new Date(2015, 7, 1)});

jsFiddle Demo

答案 1 :(得分:1)

似乎工作正常::

<div id="defaultCountdown"></div> //first
<div id="defaultCountdownSecond"></div> //second

<div id='featured_offer_countdown_template'>

     {d<}<div class='item days'>
        <span class='value'>{dn}</span>
       <span class='label'>{dl}</span>
     </div>{d>}          
    <div class='item hours'>
       <span class='value'>{hn}</span>
       <span class='label'>{hl}</span>
    </div>
    <div class='item minutes'>
       <span class='value'>{mn}</span>
        <span class='label'>{ml}</span>
    </div>
    <div class='item seconds'>
       <span class='value'>{sn}</span>
       <span class='label'>{sl}</span>
   </div>
</div>

js code:

var austDay = new Date();
austDay = new Date(austDay.getFullYear(), 5, 9);
$('#defaultCountdown').countdown({
    until: austDay,
    format: 'dHMS'    
});
$('#defaultCountdownSecond').countdown({
    until: austDay,
    format: 'dHMS'    
});
$('#year').text(austDay.getFullYear());

演示:: jsFiddle