效果:
代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> </HEAD>
<BODY> <INPUT TYPE="text" NAME="aa" id="a"> <INPUT TYPE="text" NAME="" id="b"> <INPUT TYPE="text" NAME="" id="c"> <INPUT TYPE="text" NAME="" id="d">
<SCRIPT LANGUAGE="JavaScript"> <!-- /*@ *@ 四个参数:节点,节点类型+条件(数组),事件名称( 数组)或者字符串,返回函数 *@事件类型不需要加on */ isIE=false; /*@cc_on isIE=true; @*/ function addEvent(tags,types,events,callfunction){ var tagAry=document.getElementsByTagName(tags); var len=tagAry.length; var eventslen=null; if(events.constructor==Array){ eventslen=events.length; }
for(var i=0;i<len;i++){
if(tagAry[i].getAttribute(types[0])==types[1]){ if(isIE){ if(eventslen){ for(var j=0;j<eventslen;j++){ tagAry[i].attachEvent("on"+events[j],callfunction); }
}else{ tagAry[i].attachEvent("on"+events,callfunction); }; }else{
if(eventslen){ for(var j=0;j<eventslen;j++){ tagAry[i].addEventListener(events[j],callfunction,false); }
}else{
tagAry[i].addEventListener(events,callfunction,false); } } } }
} addEvent("input",["name","aa"],"click",eventFun); function eventFun(){ alert("aaa"); }
//--> </SCRIPT> </BODY> </HTML> |