您的位置:首页 > Web前端 > JavaScript

js给对象标签添加事件&方法的封装

2015-05-31 21:39 706 查看
<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>



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: