js动态绑定事件

有关js动态绑定事件

//为元素绑定事件
function addEvent(elem,type,fn){
    if(window.addEventLister){
        elem.addEventLister(type,fn,false);
    }
    else if(window.attachEvent){
        elem.attachEvent("on"+type,fn);
    }
    else{
        elem["on" + type] = fn;
    }
}

 

//将元素和方法放到数组arrelems中
function bind(elem,fn){
    var arrtmp = new Array();
    arrtmp.push(elem)
    arrtmp.push(fn);
    arrelems.push(arrtmp);
}

 

//遍历元素
function etype(type){
    for(var i=0;i<arrelems.length;i++){
        if(document.getElementById(arrelems[i][0])!=="undefined"&&document.getElementById(arrelems[i][0])!==null){
            addEvent(document.getElementById(arrelems[i][0]),type,arrelems[i][1])
        }
    }
}

//绑定事件
function elemtype(type){
    var body = document.getElementsByTagName("body")[0];  
    etype(type);
    addEvent(body,type,function(){
        etype(type);
    });
}

 

demo:

<!DOCTYPE html>
<html>
    <head>
        <title>动态绑定事件</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <input id="btn1" type="button" value="确定" />
        <div  id="test">TODO write content</div>
        <div><span id="msg0">测试</span></div>
        <script type="text/javascript">
            var arrelems = new Array();
            //为元素绑定事件
            function addEvent(elem,type,fn){
                if(window.addEventLister){
                    elem.addEventLister(type,fn,false);
                }
                else if(window.attachEvent){
                    elem.attachEvent("on"+type,fn);
                }
                else{
                    elem["on" + type] = fn;
                }
            }
            
            //将元素和方法放到数组arrelems中
            function bind(elem,fn){
                var arrtmp = new Array();
                arrtmp.push(elem)
                arrtmp.push(fn);
                arrelems.push(arrtmp);
            }
            
            //遍历元素
            function etype(type){
                for(var i=0;i<arrelems.length;i++){
                    if(document.getElementById(arrelems[i][0])!=="undefined"&&document.getElementById(arrelems[i][0])!==null){
                        addEvent(document.getElementById(arrelems[i][0]),type,arrelems[i][1])
                    }
                }
            }

            //绑定事件
            function elemtype(type){
                var body = document.getElementsByTagName("body")[0];  
                etype(type);
                addEvent(body,type,function(){
                    etype(type);
                });
            }
        </script>

        <script type="text/javascript">
            var arrhtml = ['<span id="msg1">测试一</span>'];
            var btn1 = document.getElementById("btn1");
            addEvent(btn1,"click",function(){
                document.getElementById("test").innerHTML = arrhtml[0];
            });
            
            bind("msg0",function(){
                console.log("测试000000被点击了");  
            });
          
            bind("msg1",function(){
                console.log("测试1111111111被点击了"); 
            });
            
            elemtype("click");
        </script>
    </body>
</html>

 

 

posted @ 2013-04-15 00:33  前端咖  阅读(519)  评论(0编辑  收藏  举报