Javascript 如何使jquery计数器处理来自innerHTML的数据

Javascript 如何使jquery计数器处理来自innerHTML的数据,javascript,jquery,html,counter,Javascript,Jquery,Html,Counter,我正在尝试使用jquery创建一个计数器。当数据直接传递到div时,此计数器工作,但当使用.innerHTML调用将此数据动态附加到div时,此计数器不工作 下面我将计数器代码应用于.count类的所有元素,我希望此计数器使用document.getElementById'users.innerHTML=300对id=users的div工作;这是行不通的 但以下工作:300 document.getElementById'users'。innerHTML=300; 使用document.getE

我正在尝试使用jquery创建一个计数器。当数据直接传递到div时,此计数器工作,但当使用.innerHTML调用将此数据动态附加到div时,此计数器不工作

下面我将计数器代码应用于.count类的所有元素,我希望此计数器使用document.getElementById'users.innerHTML=300对id=users的div工作;这是行不通的

但以下工作:300

document.getElementById'users'。innerHTML=300; 使用document.getElementById'users.innerHTML=300时获得的输出;为0,但这是意外的。我有几个元素的类计数依赖于这个计数器

如果有任何帮助,我们将不胜感激。

您可能需要编写document.getElementById'users'。innerHTML=300;在.each代码之前,因为.innerHTML适用于我

document.getElementById'users'。innerHTML=300; $'.count'.eachfunction{ $this.prop'Counter',0.0动画{ 计数器:$this.text }, { 持续时间:5000, 放松:"摇摆",, 步骤:现在开始{ $this.textMath.ceilnow; } }; };
我不完全确定我想让这个计数器工作是什么意思…,但这能处理您的用例吗

const users=document.getElementByIdusers; users.innerHTML=300; //users.addEventListenerchange,console.logusers.innerHTML; $'.count'.eachfunction{ 让num=users.innerHTML;//动态设置值 $this.prop'Counter',num.animate{ 计数器:$this.text }, { 持续时间:5000, 放松:"摇摆",, 步骤:立即运行{ $this.textMath.ceilnow; } }; };

0

$('.count').each(function () {
        $(this).prop('Counter',0).animate({
            Counter: $(this).text()
        }, {
            duration: 5000,
            easing: 'swing',
            step: function (now) {
                $(this).text(Math.ceil(now));
            }
        });
     });
@Ben you need to set the innerHTML before you call the counter animation 
document.getElementById('users').innerHTML = 300;

$('.count').each(function () {
        $(this).prop('Counter',0).animate({
            Counter: $(this).text()
        }, {
            duration: 5000,
            easing: 'swing',
            step: function (now) {
                $(this).text(Math.ceil(now));
            }
        });
     });