我正在尝试将 onclick 函数附加到每个 a 标签。
我有
task.prototype.init=function(){
for (value in obj){
var Link=document.createElement('a');
Link.innerHTML='click';
Link.id=value; //I want to get the value
Link.href='#'
Link.onclick=this.changeName;
document.body.appendChild(Link);
}
}
task.prototype.changeName=function(){
//I want to get the clicked element id and I am not sure how to do it.
return false;
}
有什么办法可以实现这一点吗?
最佳答案
在事件处理程序中,this
是创建事件的对象,因此这应该可以工作:
task.prototype.changeName=function() { alert(this.id); };
关于javascript - 简单的javascript原型(prototype)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13996187/