简体   繁体   English

如何重置功能?

[英]How do I reset a function?

 var count = 0; var change = function(btn) { count++ var color = ""; switch(count) { case 1: color = "#ef2715"; break; case 2: color = "#ef8c14"; break; case 3: color = "#efd514"; break; case 4: color = "#3fef14"; break; case 5: color = "#145def"; break; case 6: color = "#093b9e"; break; case 7: color = "#6414ef"; break; default: color = "#6414ef"; break; } btn.style.background = color; } 
 button { border: 1px solid black; width: 50px; height: 50px; float: left; } 
 <button id="one" onclick="change(this)"></button> <button id="one" onclick="change(this)"></button> <button id="one" onclick="change(this)"></button> <button id="one" onclick="change(this)"></button> <button id="one" onclick="change(this)"></button> <button id="one" onclick="change(this)"></button> <button id="one" onclick="change(this)"></button> <button id="one" onclick="change(this)"></button> 

I want to make some kind of drawing canvas. 我想做些绘图画布。 If you click a button once, the color will be red and if you click twice the color will be orange. 如果单击一次按钮,则颜色将为红色,如果单击两次,则颜色将为橙色。 And this function has to be reset ('count' should be reset) when you click another button. 当您单击另一个按钮时,必须重置此功能(应重置“计数”)。

However, the function count stays even if I click another button. 但是,即使单击另一个按钮,功能计数仍保持不变。 How can I solve this problem? 我怎么解决这个问题?

(Is it necessary for me to make 50 buttons if I want my canvas to be 2500px? Or is there any easier/shorter way to make buttons? ) (如果我希望我的画布为2500px,是否有必要设置50个按钮?或者有没有更简单/更短的按钮制作方法?)

Thank you 谢谢

Try this approach. 试试这种方法。 It provides separate counters for all of your buttons. 它为所有按钮提供单独的计数器。

var change = function(btn) {
  if(!btn.hasOwnProperty('myCount')) {
    btn.myCount = 0;
  }
  btn.myCount++;
  var color = "";
  switch(btn.myCount) {
    case 1: 
      color = "#ef2715";
      break;
    // ...
  }
  btn.style.background = color;
}

Each counter is being stored right on the button object. 每个计数器都存储在按钮对象上。

UPD The shorter version is UPD的缩写是

var change = function(btn) {
  btn.myCount = !btn.myCount ? 1 : btn.myCount + 1;
  var color = "";
  // ...
}

One of the ways of doing so would be to set a custom attribute on each button - such as "data-count" which keeps track of color index for that button. 这样做的方法之一是在每个按钮上设置自定义属性,例如"data-count" ,该属性跟踪该按钮的颜色索引。

Thus when you click on the button, you can increment this count - and save it back to the data-count attribute. 因此,当您单击按钮时,可以增加此计数-并将其保存回data-count属性。

See the snippet below to see how you could implement this: 请参见下面的代码片段,以了解如何实现此功能:

 var change = function(btn) { let count = btn.attributes["data-count"] ? parseInt(btn.attributes["data-count"].value) : 0; count++; var color = ""; switch (count) { case 1: color = "#ef2715"; break; case 2: color = "#ef8c14"; break; case 3: color = "#efd514"; break; case 4: color = "#3fef14"; break; case 5: color = "#145def"; break; case 6: color = "#093b9e"; break; case 7: color = "#6414ef"; break; default: color = "#6414ef"; break; } btn.style.background = color; btn.setAttribute("data-count", count); } 
 button { border: 1px solid black; width: 50px; height: 50px; float: left; } 
 <button onclick="change(this)"></button> <button onclick="change(this)"></button> <button onclick="change(this)"></button> <button onclick="change(this)"></button> <button onclick="change(this)"></button> <button onclick="change(this)"></button> <button onclick="change(this)"></button> <button onclick="change(this)"></button> 

Also, you can reset the counter based on a condition, such as count == 7 - if required. 此外,如果需要,您可以根据条件重置计数器,例如count == 7

Elements in DOM are not prefered to be having same identity and below code works on Id which holds a logic when a continuous click happens to same button and resets for other DOM中的元素不希望具有相同的标识,并且下面的代码在ID上起作用,当连续单击同一按钮并为其他按钮重置时,该ID具有逻辑

  var count =0; var tempButtonId=""; var change = function(btn) { if(btn.id!=tempButtonId) { tempButtonId=btn.id; count=1; }else{ count++ } var color = ""; switch(count) { case 1: color = "#ef2715"; break; case 2: color = "#ef8c14"; break; case 3: color = "#efd514"; break; case 4: color = "#3fef14"; break; case 5: color = "#145def"; break; case 6: color = "#093b9e"; break; case 7: color = "#6414ef"; break; default: color = "#6414ef"; break; } btn.style.background = color; } 
 button { border: 1px solid black; width: 50px; height: 50px; float: left; } 
 <button id="one" onclick="change(this)"></button> <button id="two" onclick="change(this)"></button> <button id="three" onclick="change(this)"></button> <button id="four" onclick="change(this)"></button> <button id="five" onclick="change(this)"></button> <button id="six" onclick="change(this)"></button> <button id="seven" onclick="change(this)"></button> <button id="eight" onclick="change(this)"></button> 

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

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