Javascript 有没有一种方法可以使用计数器将不同的内容附加到div中?

Javascript 有没有一种方法可以使用计数器将不同的内容附加到div中?,javascript,jquery,Javascript,Jquery,我试图制作一个按钮,用计数器更改添加到我的targetBox的内容。如果计数器为1,则附加某些内容;如果计数器为0,则附加其他内容。如有任何帮助,将不胜感激移动条件,即单击处理程序内的块 var play = function(){ var counter = 0; if(counter == 0){ $( this ).click(function(e) { var targetBox = (e.target.id); $("#" + targetBox).append("so

我试图制作一个按钮,用计数器更改添加到我的
targetBox
的内容。如果计数器为1,则附加某些内容;如果计数器为0,则附加其他内容。如有任何帮助,将不胜感激

移动条件,即单击处理程序内的块

var play = function(){
var counter = 0;
if(counter == 0){
  $( this ).click(function(e) {
   var targetBox = (e.target.id);
   $("#" + targetBox).append("something");
   counter++;
 })
} else if (counter == 1){
  $( this ).click(function(e) {
   var targetBox = (e.target.id);
   $("#" + targetBox).append("O");
   counter--;
  });
 }
}
移动条件,即单击处理程序内的if块

var play = function(){
var counter = 0;
if(counter == 0){
  $( this ).click(function(e) {
   var targetBox = (e.target.id);
   $("#" + targetBox).append("something");
   counter++;
 })
} else if (counter == 1){
  $( this ).click(function(e) {
   var targetBox = (e.target.id);
   $("#" + targetBox).append("O");
   counter--;
  });
 }
}

将条件放入事件处理程序中:

 $( this ).click(function(e) {
   var targetBox = (e.target.id);
   if(counter == 0) {
     $("#" + targetBox).append("something");
     counter++;
   } else if(counter == 1){
     $("#" + targetBox).append("O");
     counter--;
   }
 })

将条件放入事件处理程序中:

 $( this ).click(function(e) {
   var targetBox = (e.target.id);
   if(counter == 0) {
     $("#" + targetBox).append("something");
     counter++;
   } else if(counter == 1){
     $("#" + targetBox).append("O");
     counter--;
   }
 })