这是一个迭代记录的 Django 模板。每条记录都包含一个由 JS 函数填充的 div。为了让 JS 知道要做什么,它需要从每次 for 循环迭代中获取一个变量并使用它。我不知道具体如何实现这一目标或是否可能。我不知道,也许记录在单独的 JS 对象实例中触发计时器时需要不同的方法。
---------- html --------------------------
{% for match in upcoming_matches %}
...
<tr>
<td> Match title </td>
<td> Match time </td>
<td> Starts in: </td>
<tr>
<tr>
<td> {{ match.title }} </td>
<td> {{ match.start_time }} </td>
<td> <div id="matchCountdown"/></td>
<tr>
...
{% endfor %}
------------ JS ---------------------------
$(function () {
var start_date = {{ match.start_date }}; // Obviously I can't access vars from for loop, I could access upcoming_matches but not the ones from for loop
var matchDate = new Date(start_date.getTime()
$('#matchCountdown').countdown({until: matchDate});
});
您还可以在代码的 javascript 部分使用 {% for %} 循环。如果你在 html 模板中这样写:
<script type="text/javascript">
$(function() {
{% for match in upcoming_matches %}
var match_date_{{ match.id }} = new Date({{ match.start_date }}).getTime();
$('#matchCountdown_{{ match.id }}').countdown({until: match_date_{{ match.id }});
{% endfor %}
});
</script>
Also, <div id="matchCountdown"/>
变成<div id="matchCountdown_{{ match.id }}"/>
在这种情况下。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)