如何让 countUp Javascript 包工作?

How do I get countUp Javascript package working?

我正在尝试实施 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

只需将下行中的 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 节点。