Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中使用.each()来显示不同的计数器_Javascript_Jquery - Fatal编程技术网

如何在javascript中使用.each()来显示不同的计数器

如何在javascript中使用.each()来显示不同的计数器,javascript,jquery,Javascript,Jquery,我有一个显示计数器的javascript函数。计时器从总值的50%开始,当达到95%时,计数器将减速。这工作正常,但我需要显示具有不同值的多个计数器。这是我的 html <span class="counter" data-count="100"></span><br/> <span class="counter" data-count="200"></span><br/> <span class="counter"

我有一个显示计数器的javascript函数。计时器从总值的50%开始,当达到95%时,计数器将减速。这工作正常,但我需要显示具有不同值的多个计数器。这是我的

html

<span class="counter" data-count="100"></span><br/>
<span class="counter" data-count="200"></span><br/>
<span class="counter" data-count="300"></span><br/>

一个解决办法是使用

$('counter').each(function(index, element) {
    // here you declare local variables and functions.
}
您可以使用几乎所有的代码。唯一的区别是变量和函数必须是局部的,在de函数中

检查

还有一件事:尝试使用var myVariable来声明变量。也许您可以使用Sublime Text 3和jslint插件进行在线javascript语法检查。这与您的问题无关,但当您使用JavaScript编写代码时,这是一个很好的实践


你好

是否将计数与元素绑定?
$('counter').each(function(index, element) {
    // here you declare local variables and functions.
}