代码太长.又写了短的代码来说明一下.xu = function(element){
this.eid = document.getElementById(element);
this.eid.onfocus = this.show;
}
xu.prototype.eid = null;
xu.prototype.show = function(){
alert(this.eid.id);
}<input type="text" name="User_Name" id="User_Name" value="" size="32" />
<input type="password" name="User_Password" id="User_Password" value="" size="32" />
alert(xu.prototype.eid);
var a1 = new xu("User_Name");
var a2 = new xu("User_Password");
a1.show();
a2.show();
alert(xu.prototype.eid);
运行时都是正常的.手工执行"a1.show();"时对话框中能显示各自的ID值. 但是在绑定的事件里却显示this.eid 为 "undefined".网页错误详细信息消息: 'this.eid.id' 为空或不是对象
行: 11
字符: 2
代码: 0
URI: http://localhost/fapm/xuScript/Test.js
this.eid = document.getElementById(element);
this.eid.onfocus = this.show;
}
xu.prototype.eid = null;
xu.prototype.show = function(){
alert(this.eid.id);
}<input type="text" name="User_Name" id="User_Name" value="" size="32" />
<input type="password" name="User_Password" id="User_Password" value="" size="32" />
alert(xu.prototype.eid);
var a1 = new xu("User_Name");
var a2 = new xu("User_Password");
a1.show();
a2.show();
alert(xu.prototype.eid);
运行时都是正常的.手工执行"a1.show();"时对话框中能显示各自的ID值. 但是在绑定的事件里却显示this.eid 为 "undefined".网页错误详细信息消息: 'this.eid.id' 为空或不是对象
行: 11
字符: 2
代码: 0
URI: http://localhost/fapm/xuScript/Test.js
alert(this.eid.id);
}这个this执行的时候 已经指向了 被绑定事件的对象
this.eid = document.getElementById(element);
this.eid.onfocus = this.show;
}
//你将xu.prototype.eid 设置成null
xu.prototype.eid = null;xu.prototype.show = function(){
//这里的this指向的是xu对象,
alert(this.eid.id);
}实际的alert(this.eid.id);
执行的是alert(null.id);
先前试过,用事件里加参数。但也出现了问题。(不使用new 构造 xu 没有问题。奇怪~)xu = function(element){
this.eid = document.getElementById(element);
//this.eid.onfocus = this.show;
if (this.eid.addEventListener) {
// IE
alert(this.eid.addEventListener)
this.eid.addEventListener("focus", function(event){
this.show(event, this.eid)}, false);
} else if (this.eid.attachEvent) {
alert("attachEvent");
// Other
this.eid.attachEvent('onfocus', function(){
this.show(this.eid)});
}
}