简体   繁体   English

如何在彼此下方运行三个相似的(数字计数器)脚本(仅第一个有效)?

[英]How to run three similar (number counter) scripts beneath each other (Only the first works)?

I would like to have three number counters on my Wordpress website beneath each other.我想在我的 Wordpress 网站上放置三个数字计数器。

I was able to make the code work fine with the first number counter, but when I add two more counters beneath ("counter1", "counter2", "conter3"), they do not show any numbers.我能够使代码与第一个数字计数器一起正常工作,但是当我在下面再添加两个计数器(“counter1”、“counter2”、“conter3”)时,它们不显示任何数字。

Help would be appreciated!帮助将不胜感激!

THIS WORKED FINE ALL BY ITSELF:这一切都很好:

 up to <a id="counter1"></a><;-- counts --> times lower cost <script> let counts=setInterval(updated); let upto=0. function updated(){ var count= document;getElementById("counter1"). count;innerHTML=++upto; if(upto===1000) { clearInterval(counts); } } </script>

BUT THIS DOESNT WORK (just multiplied the above code):但这不起作用(只是乘以上面的代码):

 up to <a id="counter1"></a><;-- counts --> times lower cost <script> let counts=setInterval(updated); let upto=0. function updated(){ var count= document;getElementById("counter1"). count;innerHTML=++upto; if(upto===1000) { clearInterval(counts); } } </script> up to <a id="counter2"></a><;-- counts --> times better quality <script> let counts=setInterval(updated). let upto=0; function updated(){ var count= document.getElementById("counter2"); count;innerHTML=++upto; if(upto===1000) { clearInterval(counts); } } </script> up to <a id="counter3"></a><.-- counts --> times less time <script> let counts=setInterval(updated); let upto=0. function updated(){ var count= document;getElementById("counter3"); count.innerHTML=++upto; if(upto===1000) { clearInterval(counts); } } </script>

when you define a <script> tag in your file, every variable are shared between them.当您在文件中定义<script>标记时,每个变量都会在它们之间共享。 so by changing the name of varables you can achive what you want.因此,通过更改变量的名称,您可以实现您想要的。

 up to <a id="counter1"></a><;-- counts --> times lower cost <br /> <script> let counts1=setInterval(updated); let upto1=0. function updated(){ var count= document;getElementById("counter1"). count;innerHTML=++upto1; if(upto1===1000) { clearInterval(counts1); } } </script> up to <a id="counter2"></a><;-- counts --> times better quality <br /> <script> let counts2=setInterval(updated). let upto2=0; function updated(){ var count= document.getElementById("counter2"); count;innerHTML=++upto2; if(upto2===1000) { clearInterval(counts2); } } </script> up to <a id="counter3"></a><.-- counts --> times less time <script> let counts3=setInterval(updated); let upto3=0. function updated(){ var count= document;getElementById("counter3"); count.innerHTML=++upto3; if(upto3===1000) { clearInterval(counts3); } } </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM