本文介绍了jQuery multi-countdown .each()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在页面上进行多次倒计时,

I'm trying to make a multi-countdown on a page, which looks like :

<table>
  <tr id="4236377487">
    <td class="remain"></td>
    <td>Something</td>
  </tr>
  <tr id="768769080">
    <td class="remain"></td>
    <td>Something else</td>
  </tr>
</table>

倒数必须放在:

<td class="remain"><!-- countdown --></td>

每个倒数都从行ID值开始.这是我的代码,但是不起作用:

Each countdown starts with the row id value. Here's my code, but it doesn't work :

$(document).ready(function(){
  $('.remain').each(function () {
     var count = $(this).attr("id");
     countdown = setInterval(function(){
     $(this).html(count + " seconds remaining!");
     if (count == 0) {
       //do something
     }
     count--;
     }, 1000);
  });
});

感谢您的帮助:)

Fabien

推荐答案

$(document).ready(function(){
  $('tr[id]').each(function () {
     var $this = $(this);
     var count = parseInt($this.attr("id"));
     countdown = setInterval(function(){
         $('.remain', $this).html(count + " seconds remaining!");
         if (count-- == 0) {
           //do something
           clearInterval(countdown);
         }
     }, 1000);
  });
});

在这里尝试: http://jsfiddle.net/moeishaa/PwG45/

这篇关于jQuery multi-countdown .each()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 23:05