【问题标题】:How do I get countUp Javascript package working?如何让 countUp Javascript 包正常工作?
【发布时间】:2017-12-13 07:57:15
【问题描述】:

我正在尝试实现 countUp Javascript 包,但出现错误:[CountUp] 目标为空或未定义。 countUp Javascript 包的 gitHub 存储库是 https://github.com/inorganik/CountUp.js。对于有此软件包经验的个人,我的问题是如何修复我的代码以避免此错误?代码如下:

<html>
<title>Count Up Test</title>
<head>
<script src="countUp.js"></script>

</head>
<body>
<h1 id="tester">100</h1>

<script>

var options = {
    useEasing: true,
    useGrouping: true,
    separator: ',',
    decimal: '.',
};

tester1 = document.getElementById('tester').innerHTML;
console.log(tester1);

var test1 = new CountUp('tester1', 100, 1500, 0, 2.5, options);
if (!test1.error) {
    test1.start();
} else {
    console.error(test1.error);
}

</script>
</body>
</html>

我尝试为 DOM 添加一个 eventListener,我尝试使用 window.onload,console.log(tester1) 显示 100

【问题讨论】:

  • 不知道为什么这个问题被否决了。如果投反对票的人提供一些反馈会很有帮助。

标签: javascript packages


【解决方案1】:

只需在下面的行中将tester1 更改为tester....

var test1 = new CountUp('tester', 100, 1500, 0, 2.5, options);

假设你有一个像这样的 html 元素......

<h1 id="tester">100</h1>

请注意,html 元素的 id 与传递给 CountUp ("tester") 的字符串匹配

CountUp 的构造函数的第一个参数是一个字符串,表示您要使用的 html 元素的id 属性。基本上,CountUp 会自己调用 getElementById 并为您查找 DOM 节点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 2015-05-08
    • 2020-08-27
    • 2018-06-11
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多