执行顺序有问题,你调用gI.init()时
<input type= "text " id= "inp1 "/> <br/>
<input type= "text " id= "inp2 "/>
还没有生成
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml "> <head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
<title> prototype </title>
<script type= "text/javascript ">
function getInput(name,objID1,objID2){
this.name=name;
this.obj1=objID1;
this.obj2=objID2;
}
getInput.prototype.init=function(){
document.getElementById(this.obj2).onkeyup=this.prn;//该条出错,说为空或不是对象,想知道如何能把input对象以参数形式传入并定义事件等
}
getInput.prototype.prn=function(){
alert(document.getElementById(this.obj1).value+ "& "+document.getElementById(this.obj2).value);
} </script>
</head> <body>
<input type= "text " id= "inp1 "/> <br/>
<input type= "text " id= "inp2 "/>
<script>
var gI=new getInput( 'gI ', 'inp1 ', 'inp2 ');
gI.init();
</script>
</body>
</html>
<input type= "text " id= "inp1 "/> <br/>
<input type= "text " id= "inp2 "/>
还没有生成
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml "> <head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
<title> prototype </title>
<script type= "text/javascript ">
function getInput(name,objID1,objID2){
this.name=name;
this.obj1=objID1;
this.obj2=objID2;
}
getInput.prototype.init=function(){
document.getElementById(this.obj2).onkeyup=this.prn;//该条出错,说为空或不是对象,想知道如何能把input对象以参数形式传入并定义事件等
}
getInput.prototype.prn=function(){
alert(document.getElementById(this.obj1).value+ "& "+document.getElementById(this.obj2).value);
} </script>
</head> <body>
<input type= "text " id= "inp1 "/> <br/>
<input type= "text " id= "inp2 "/>
<script>
var gI=new getInput( 'gI ', 'inp1 ', 'inp2 ');
gI.init();
</script>
</body>
</html>
解决方案 »
- 在.js里面怎么实现自动补全
- 正则表达式 (\w){3} 怎么理解
- javascript读xml文件根据text获取上一个节点的text
- dh20156风之石申请JavaScript版版主
- Scripting.FileSystemObject 为什么不能保存文件到网络共享路径?而只能保存到C:\\ ?
- innerHTML用法
- 如何刷新子窗口,
- 提交问题。如果一个网页中有一个名为submit,我想自动提交这个网页怎样实现。如代码如下。
- 大神去哪儿?!我自己组装的json数据 传到后台 会部分丢失 是为什么
- JS如何操作iframe中某元素绑定的事件
- 关于javascript setInterval 计时器的一个非常奇怪的问题
- 如何在打开一个页面时,自动向另外一个页面发送2次回车键,并且间隔在10秒
alert(document.getElementById(this.obj1).value+ "& "+document.getElementById(this.obj2).value);
} 而且你的这个函数执行时,此时的this对象指向的是<input type="text" id="inp2"/> ,
而非gI对象了this是谁执行这个函数就指向谁
getInput.prototype.prn=function(){
alert(document.getElementById(this.obj1).value+ "& "+document.getElementById(this.obj2).value); //说缺少对象
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> prototype </title>
<script type="text/javascript">
function getInput(name,objID1,objID2){
this.name=name;
this.obj1=objID1;
this.obj2=objID2;
}
getInput.prototype.init = function(){
document.getElementById(this.obj2).onkeyup = (function () {
var wc = this;
return function (event) {
wc.prn(event);//该条出错,说为空或不是对象,想知道如何能把input对象以参数形式传入并定义事件等
}
}).call(this);
}
getInput.prototype.prn = function(){
alert(document.getElementById(this.obj1).value+"&"+document.getElementById(this.obj2).value);
} window.onload = function () {
var gI = new getInput('gI','inp1','inp2');
gI.init();
}
</script>
</head><body>
<input type="text" id="inp1"/><br/>
<input type="text" id="inp2"/>
</body> </html>
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> prototype </title></head><body>
<input type="text" id="inp1"/> <br/>
<input type="text" id="inp2"/>
</body></html>
<script type="text/javascript">
function getInput(name,objID1,objID2){
this.name=name;
this.obj1=objID1;
this.obj2=objID2;
}
getInput.prototype.init=function(){
var This = this;
this.prn=function(){
alert(document.getElementById(This.obj1).value+"&"+document.getElementById(This.obj2).value);
}
document.getElementById(this.obj2).onkeyup=this.prn;//该条出错,说为空或不是对象,想知道如何能把input对象以参数形式传入并定义事件等
}
var gI=new getInput('gI','inp1','inp2');
gI.init();
</script>
getInput.prototype.init = function(){
document.getElementById(this.obj2).onkeyup = (function () {
var wc = this;
return function (event) {
wc.prn(event);
}
}).call(this);
}
这样执行函数的话
this是指向window的。所以需要用call改变下this的指向
这样执行的这个函数里,this就是gI了
然后创建一个局部变量wc 值是对gi的引用
然后返回一个函数。。
函数里调用到了那个变量wc所以产生闭包
getInput.prototype.init = function(){
var wc = this;
document.getElementById(this.obj2).onkeyup = function () {
wc.prn(event);
};
};这样写也可以的:D