87,910
社区成员
发帖
与我相关
我的任务
分享
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';});
}
}
<!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>
<!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>
<!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>