简体   繁体   English

Javascript多重倒数计时

[英]Javascript multiple timer countdowns

I have a page which is a list of products loaded from the database. 我有一个页面,其中列出了从数据库加载的产品。 Each product has a date field that is also loaded from the database. 每个产品都有一个日期字段,该日期字段也从数据库中加载。 Each product is contained within a div and a countdown timer script is also loaded within that div. 每个产品都包含在div中,并且倒计时计时器脚本也已加载到该div中。 The date associated with the product is used to count the timer down. 与产品关联的日期用于倒计时。 This date is set to a hidden field with the div which the script can access.So each product div basically looks like this: 该日期设置为脚本可以访问的div的隐藏字段,因此每个产品div基本上如下所示:

<div id="prod1">
<input type="hidden" value"targetdate1"/>
<script type="text/javascript" src="countdown.js"></script>
</div>

<div id="prod2">
<input type="hidden" value"targetdate2"/>
<script type="text/javascript" src="countdown.js"></script>
</div>

<div id="prod3">
<input type="hidden" value"targetdate3"/>
<script type="text/javascript" src="countdown.js"></script>
</div>

<div id="prod4">
<input type="hidden" value"targetdate4"/>
<script type="text/javascript" src="countdown.js"></script>
</div>
 .......

So, essentially the script will be loaded multiple times in a page. 因此,基本上,脚本将在页面中多次加载。 And each script should only read the hidden field contained within its own div. 并且每个脚本应仅读取其自身div中包含的隐藏字段。 I tried to find a way to do this using jquery selectors/javascript but nothing works. 我试图找到一种使用jquery选择器/ javascript做到这一点的方法,但是没有任何效果。

Try following code in your countdown.js : 尝试在您的countdown.js中遵循以下代码:

document.getElementsByTagName("script")[pos].parentElement  // pos will the serial number of the script tag

Now once the div is seelected you can work on it. 现在,一旦选择了div,就可以对其进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM