jquery多个keith-wood倒计时插件在一个页面上
问题描述:
I am using the jQuery Keith-Wood plugin for showing one countdown timer on my page, and it works fine.
Now I want to display multiple countdowns, but it doesn't seem to work (it works only for the first element).
Here is what i do:
<? foreach ($sales as $sale): ?>
<script>
$(function () {
var austDay = new Date();
austDay = new Date(<?php echo date('Y', strtotime($sale->end))?>,<?php echo (date('m', strtotime($sale->end))-1)?>,<?php echo date('d-1', strtotime($sale->end))?>,<?php echo date('h', strtotime($sale->end))?>,<?php echo date('i', strtotime($sale->end))?>,<?php echo date('s', strtotime($sale->end))?>);
$('#defaultCountdown').countdown({until: austDay, format: 'dHMS'});
});
</script>
<div id="defaultCountdown"></div>
Any idea about how could I could make it work for each sale data? I mean, how can I make it multiple and working?
我使用jQuery Keith-Wood插件在我的页面上显示一个倒数计时器,它运行正常。
现在我想要显示多个倒计时,但它似乎不起作用(它仅适用于第一个元素)。 p>
这是什么 我这样做: p>
&lt;? foreach($ sales as $ sale):?&gt;
&lt; script&gt;
$(function(){
var austDay = new Date();
austDay = new Date(&lt;?php echo date( 'Y',strtotime($ sale-&gt; end))?&gt;,&lt;?php echo(date('m',strtotime($ sale-&gt; end)) - 1)?&gt;,&lt;? php echo date('d-1',strtotime($ sale-&gt; end))?&gt;,&lt;?php echo date('h',strtotime($ sale-&gt; end))?&gt;,&lt; ;?php echo date('i',strtotime($ sale-&gt; end))?&gt;,&lt;?php echo date('s',strtotime($ sale-&gt; end))?&gt;);
$('#defaultCountdown')。倒计时({until:austDay,格式:'dHMS'});
});
&lt; / script&gt;
&lt; div id =“defaultCountdown”&gt; &lt; / div&gt;
code> pre>
我对如何使其适用于每个销售数据有任何想法? 我的意思是,我怎样才能使它成为多个并且有效? p>
div>
答
You need to uniquely identify each element. If there is a sale id i'd do it like this:
$('#defaultCountdown<?php echo $sale->Id?>').countdown(...);
...
<div id="defaultCountdown<?php echo $sale->Id?>"></div>
Or just use a counter:
<?
$count = 0;
foreach ($sales as $sale):
$count++;
?>
$('#defaultCountdown<?php echo $count ?>').countdown(...);
...
<div id="defaultCountdown<?php echo $count ?>"></div>