为什么var newid中的var idnumber不计数?
var idnumber = 1;
var newid = "box-"+idnumber;
var newbox = "<div id='" + newid + "' class='box'></div>";
$( document ).ready(function() {
$('#add').click(function(){
idnumber++;
console.log(idnumber+" "+newid+" "+newbox);
});
});
有关示例,请参见:http://jsfiddle.net/vincentweltje/jx08mwe7/
最佳答案
那是因为你设定
var newid = "box-"+idnumber;
var newbox = "<div id='" + newid + "' class='box'></div>";
一次,这样,如果您第二次单击,您的变量将不会计数。
您应该将2个变量放入.click函数中,如下所示:
$('#add').click(function(){
idnumber++;
var newid = "box-"+idnumber;
var newbox = "<div id='" + newid + "' class='box'></div>";
console.log(idnumber+" "+newid+" "+newbox);
});