抱歉没有给出明确的标题,因为我不知道为什么我的脚本不起作用。
var all=[];
function People(name){
this.name=name;
this.func=function(){alert(this.name)};
all.push(this);
};
var person1=new People('Peter');
for(i=0;i<all.length;i++){
var newBtn=document.createElement('input');
document.body.appendChild(newBtn);
newBtn.type='button';
newBtn.value=all[i].name;
newBtn.onclick=all[i].func; // why doesn't is say "Peter" when I click the button ?
};
顺便说一句,有没有更好的方法来实现我的目标:创建一些对象;为每个对象创建一个按钮;单击按钮时,执行某些功能。