<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>封装自己的getClass</title> </head> <body> <input type="submit" name="confirm" value="提交"> <script> window.onload = function(){ function getClass(name){ var Oname = document.getElementsByName(name); for(var i in Oname){ return Oname[i]; } } function addEvent(ev, type){ var ev = ev || window.event; if(ev.addEventListener){ ev.addEventListener(type, function(){ alert(ev.type); }, false); }else if(ev.attachEvent){ ev.attachEvent("on" + type, function(){ alert("ev.attachEvent"); }); } } //alert(getClass("confirm")); //Object HTMLinputElement addEvent(getClass("confirm"), 'click'); } </script> </body> </html>