function addEvent(){
var inputAll = document.getElementsByTagName("INPUT");
for(var i=0; i<inputAll.length; i++) {
inputAll[i].attachEvent("onclick",function (){alert('ddd');});
}}这样测试是没问题的
var inputAll = document.getElementsByTagName("INPUT");
for(var i=0; i<inputAll.length; i++) {
inputAll[i].attachEvent("onclick",function (){alert('ddd');});
}}这样测试是没问题的
我帮你写了下面测试代码<!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>
<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>
attachEvent这个方法是只能在IE里用吗?
如果是的话,e=e||event是不是就不用了,直接使用event就可以了吧?
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>