js給物件標籤新增事件&方法的封裝
阿新 • • 發佈:2019-01-23
<pre name="code" class="html"><span style="font-family:Microsoft YaHei;font-size:14px;"><body> <div id="box"> <input type="button" value="test" id="input1"/> </div> <script> function fun1(){ alert("hello"); } var input = document.getElementById("input1"); input.addEventListener("click",fun1,false); input.addEventListener("click",function(){alert("hello")},false);//removeEventListener //input.attachEvent("onclick",function(){alert("hello")},false);//detachEvent IE8 onclick //封裝方法 var funUtil = { addEvent:function(type,fun,element){ element.addEventListener(type,fun,false); }, removeEvent:function(type,fun,element){ element.removeEventListener(type,fun,false); } }; funUtil.addEvent("click",fun1,input); funUtil.removeEvent("click",fun1,input); </script> </body></span>