attachEvent的问题,请大家看看,谢谢了~~

zhl1015 2008-04-24 11:36:30

for(var i=0; i<inputAll.length; i++) {
var inputAll = document.getElementsByTagName("INPUT");
for(var i=0; i<inputAll.length; i++) {
inputAll[i].attachEvent("onclick", function() {this.value='aabbcc';});
}
}


这里的this关键字传不进去,不起作用,直接换成inputAll[i].value='aabbcc';还报错,不行了~~请高手给个写法吧,实在搞不出来了,谢谢了~~
...全文
91 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
rjzou2006 2008-04-24
  • 打赏
  • 举报
回复
主要的是inputAll变量undefined


我帮你写了下面测试代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="zourinet">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

</head>
<body>
<input type="checkbox" name="" value="">
<input type="checkbox" name="" value="">
<input type="checkbox" name="" value="">
<input type="checkbox" name="" value="">
<input type="checkbox" name="" value="">
</body>
<script language="javascript">
var inputAll = document.getElementsByTagName("INPUT");
for(var i=0; i<inputAll.length; i++) {
for(var i=0; i<inputAll.length; i++) {
inputAll[i].attachEvent("onclick", function() {this.value="abc";alert(this.value);});
}}

</script>
</HTML>
strife013 2008-04-24
  • 打赏
  • 举报
回复
不会阿
lawrendc 2008-04-24
  • 打赏
  • 举报
回复
你用的什么浏览器? firefox?
lawrendc 2008-04-24
  • 打赏
  • 举报
回复
function addEvent(){
var inputAll = document.getElementsByTagName("INPUT");
for(var i=0; i<inputAll.length; i++) {
inputAll[i].attachEvent("onclick",function (){alert('ddd');});
}

}

这样测试是没问题的
lawrendc 2008-04-24
  • 打赏
  • 举报
回复
向showbo学习
Go 旅城通票 2008-04-24
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zhl1015 的回复:]
回5楼 showbo
attachEvent这个方法是只能在IE里用吗?
如果是的话,e=e ¦ ¦event是不是就不用了,直接使用event就可以了吧?
[/Quote]

attachEvent只能在ie下用,ff是addEventListener,并且事件的on前缀不需要

e=e||event 这样写是兼容ff的

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="zourinet">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

</head>
<body>
<input type="text" name="">
<input type="text" name="">
<input type="text" name="">
<input type="text" name="">
<input type="text" name="">
</body>
<script language="javascript">
var inputAll = document.getElementsByTagName("INPUT");
for(var i=0; i<inputAll.length; i++) {
for(var i=0; i<inputAll.length; i++) {
//其实onclick就好了,兼容了ff和ie,并且此时的this为当前的input,如果使用attachEvent时则this对象为window,所以出错
// inputAll[i].onclick=function(){this.value="aabbcc"};

//下面的代码也可以
if(inputAll[i].attachEvent)//ie
inputAll[i].attachEvent("onclick", function(){event.srcElement.value="aabbcc";});
else//ff或者w3c
inputAll[i].addEventListener("click", function(e){e.target.value="aabbcc";},false);
}}

</script>
</HTML>
zhl1015 2008-04-24
  • 打赏
  • 举报
回复
回5楼 showbo
attachEvent这个方法是只能在IE里用吗?
如果是的话,e=e||event是不是就不用了,直接使用event就可以了吧?
Go 旅城通票 2008-04-24
  • 打赏
  • 举报
回复
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="zourinet">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

</head>
<body>
<input type="text" name="">
<input type="text" name="">
<input type="text" name="">
<input type="text" name="">
<input type="text" name="">
</body>
<script language="javascript">
var inputAll = document.getElementsByTagName("INPUT");
for(var i=0; i<inputAll.length; i++) {
for(var i=0; i<inputAll.length; i++) {
inputAll[i].attachEvent("onclick", function(e){e=e||event;(e.srcElement||e.target).value="aabbcc";});
}}

</script>
</HTML>

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧