随笔 - 11  文章 - 2  评论 - 1

封装自己的getClass

<!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>

 

posted @ 2016-06-18 10:15  Double405  阅读(211)  评论(0编辑  收藏  举报