点击div标签,里面的文字由“1”变为“加油”,可是有什么我注释掉addEventListener, 处理函数clicklistener(event)只是作为参数传入addEvent(element, event, listener), 但是没调用呀,还是会有效果 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>封装小jQuery库实现js事件</title> </head> <body> <div id="doma" style="height: 100px;width: 100px;background-color: blue;">1</div> <script type="text/javascript"> // 仅能根据id捕获dom的封装 function $(selector){ if (selector.substr(0,1) == "#") { var getDom = document.getElementById(selector.substring(1)); return getDom; } else{ console.log("This is not a id"); } } // 给一个element绑定一个针对event事件的响应,响应函数为listener function addEvent(element, event, listener) { // element.addEventListener(event,listener); //为什么我这行注释掉,clicklistener只是作为参数传入,没有调用,可是刷新后还是默认显示“加油” } // 例如: function clicklistener(event) { var div1 = document.getElementById("doma") div1.firstChild.nodeValue = "加油!"; } addEvent($("#doma"), "click", clicklistener()); </script> </body> </html>
ziom