简体   繁体   English

图片和计数器不能与多个计数器一起使用

[英]Image and counter not working with more than one counter

So I have this counter on my HTML page that goes up every time this 'thumps up' image is clicked, and when the 'thumbs down' image is clicked, the counter goes down. 因此,我的HTML页面上有一个计数器,每次单击此“重击”图像时该计数器都会上升,而单击“拇指向下”的图像时,计数器将下降。 Max value is 10. This works fine. 最大值为10。这可以正常工作。 Like this: HTML 像这样: HTML

   <img src="images/thumb-up.png" onclick="CountUp1();" id="votingup1" />
   <span id="counter1">0</span>
   <img src="images/thumb-down-dark.png" onclick="CountDown1();" id="votingdown1" />

JavaScript 的JavaScript

<script type="text/javascript">
     var minVal = 0, maxVal = 10, clicks = 0,
  display = document.getElementById("counter1");

     function CountUp1() {
         clicks = Math.min(maxVal, clicks + 1);
         display.innerHTML = clicks;
     }

     function CountDown1() {
         clicks = Math.max(minVal, clicks - 1);
         display.innerHTML = clicks;
     }
     </script>

And that works fine! 而且效果很好!

So then I tried adding another section of this voting system. 因此,我尝试添加此投票系统的另一部分。 Have different function names obviously and different id's for the images and counter. 图像和计数器具有明显不同的功能名称和不同的ID。 Like this. 像这样。

 <script type="text/javascript">
     var minVal = 0, maxVal = 10, clicks = 0,
     display = document.getElementById("counter2");

     function CountUp2() {
         clicks = Math.min(maxVal, clicks + 1);
         display.innerHTML = clicks;
     }

     function CountDown2() {
         clicks = Math.max(minVal, clicks - 1);
         display.innerHTML = clicks;
     }

</script>

But that didn't work. 但这没有用。 If I click thumbs up on the first one, the second counter increased. 如果我在第一个计数器上单击大拇指,第二个计数器就会增加。 I did have this in one script, but it didnt work so then I put them in seperate script tags like above. 我的确在一个脚本中包含了此脚本,但是它没有用,因此我将它们放在如上的单独脚本标签中。 I also tried calling the different variables, so for example 我也尝试调用不同的变量,例如

`<script type="text/javascript">
         var minVal2 = 0, maxVal2 = 10, clicks2 = 0,
         display = document.getElementById("counter2");

         function CountUp2() {
             clicks2 = Math.min(maxVal2, clicks2 + 1);
             display.innerHTML = clicks2;
         }

         function CountDown2() {
             clicks2 = Math.max(minVal2, clicks2 - 1);
             display.innerHTML = clicks2;
         }

    </script> `

but that doesn't seem to work either. 但这似乎也不起作用。 I'm a bit stuck. 我有点卡住了。 Can anyone help. 谁能帮忙。 Thanks 谢谢

edit: the second html is as followed: 编辑:第二个html如下:

 <img src="images/thumb-up.png" onclick="CountUp2();" id="votingup2" />
 <span id="counter2">0</span>
 <img src="images/thumb-down.png" onclick="CountDown2();" id="votingdown2" />

You have to move display = document.getElementById("counter2"); 您必须移动display = document.getElementById("counter2"); into each function The assignment of display is done on page load, doesn't matter in two script sections or in a single one. 每个功能的显示分配是在页面加载时完成的,与两个脚本节或单个脚本节无关。

display = document.getElementById("counter1");
display = document.getElementById("counter2"); // overrides global 

display variable, like: 显示变量,例如:

<script type="text/javascript">
     var minVal2 = 0, maxVal2 = 10, clicks2 = 0,


     function CountUp2() {
         clicks2 = Math.min(maxVal2, clicks2 + 1);
         var display = document.getElementById("counter2");
         display.innerHTML = clicks2;
     }

     function CountDown2() {
         clicks2 = Math.max(minVal2, clicks2 - 1);
         var display = document.getElementById("counter2");
         display.innerHTML = clicks2;
     }

</script> `

Or use display2 instead of display on the second counter. 或使用display2代替第二个计数器上的display

That's because these lines: 这是因为这些行:

var minVal = 0, maxVal = 10, clicks = 0,
display = document.getElementById("counter1");

And also: 并且:

var minVal = 0, maxVal = 10, clicks = 0,
display = document.getElementById("counter2");

Are outside the functions, and thus global.. thus they overwrite each other. 在功能之外,因此是全局的。因此,它们彼此覆盖。 Perhaps Have only one version of each CountUp() and CountDown(), but allow a parameter that specifies the number of the counter. 也许每个CountUp()和CountDown()都只有一个版本,但是允许使用一个参数来指定计数器的编号。

Edit: I think you would have had it if you had also changed the display variable to display1 and display2. 编辑:我想如果您还已经将显示变量更改为display1和display2,您将拥有它。

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

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