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--;
});
}
}
我正在尝试制作一个按钮,通过计数器更改附加到我的 targetBox
的内容。如果计数器为 1,则附加一些内容;如果计数器为 0,则附加一些其他内容。任何帮助将不胜感激
最佳答案
在点击处理程序内移动条件,即 if block 。
var play = function() {
var counter = 0;
//Use proper selector here
$(this).click(function(e) {
var targetBox = (e.target.id);
if (counter == 0) {
counter++;
$("#" + targetBox).append("something");
} else if (counter == 1) {
counter--;
$("#" + targetBox).append(O);
}
});
}
关于javascript - 有没有办法用计数器将不同的东西附加到 div 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32820201/