我有一个 ID 为“orangeButton”的 div,每次单击它时都会创建一个新的 div。这工作正常,但是...我希望每个新创建的 div 都有一个增量编号添加到它的 ID 中。
我不知道该怎么做。
这是我迄今为止带有注释的代码的小提琴。
http://jsfiddle.net/taoist/yPrab/1/ http://jsfiddle.net/taoist/yPrab/1/
谢谢
JavaScript 代码
var applicationArea = document.getElementById("applicationArea");
var orangeButton = document.getElementById("orangeButton");
orangeButton.onclick = function() {
var newDivThingy = document.createElement("div");
newDivThingy.id = 'newDivThingy'; // I want each newly created div to have a numeric value concatenated to it's ID. IE newDivThingy1 newDivThingy2 newDivThingy3
applicationArea.appendChild(newDivThingy);
};
我错过了什么吗,为什么不使用计数器呢?
var counter = 0;
button.onclick = function(){
var newDivThingy = document.createElement("div");
newDivThingy.id = 'newDivThingy' + (++counter);
// continue your stuff here
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)