<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>
js给对象标签添加事件&方法的封装
最新推荐文章于 2023-06-27 17:34:14 发布