对不起没有明确的标题,因为我不知道为什么我的脚本不起作用。为什么我的功能不正确?
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 ?
};
顺便说一下,有没有更好的方法来实现我的目标:创建一些对象;与每个对象一起,创建一个按钮;当一个按钮被点击时,做一些功能。
@KirenSiva通常是一个很好的问题,但不需要大声呼喊。 :) –
@KirenSiva他没有说这是在抛出一个错误,他说当他点击按钮时并没有提醒'彼得'。 – Barmar