您好,登录后才能下订单哦!
event.js文件
//跨浏览器事件处理程序【根据能力检测调用】
var EventObject = {
//添加事件
AddEvent:function(obj , type , eventFn){
if(obj.addEventListener){//常用
obj.addEventListener(type , eventFn , false);
}
else if(obj.attachEvent){//IE8及以下
obj.attachEvent('on'+type , eventFn);
}
else{//DOM零级事件处理
obj['on'+type] = eventFn; //obj.onclick === obj['onclick']
}
},
//删除事件
DelEvent:function(obj , type , eventFn){
if(obj.removeEventListener){
obj.removeEventListener(type , eventFn , false);
}
else if(obj.detachEvent){
obj.detachEvent('on'+type , eventFn);
}
else{
obj['on'+type] = null;
}
},
//获取事件对象
GegEvent:function(event){
return event ? event : window.event;
},
//获取事件类型
GetType:function(event){
return event.type;
},
//获取事件目标
GetTarget:function(event){
return event.target || event.srcElement;
},
//阻止事件默认行为
PreventDefault:function(event){
if(event.preventDefault){
event.preventDefault();
}
else{
event.returnValue = false; //IE8及以下的版本
}
},
//阻止事件冒泡
StopPropagation:function(event){
if(event.stopPropagation){
event.stopPropagation();
}
else{
event.cancelBubble = true; //IE8及以下的版本
}
}
};
index.html页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>事件对象封装调用</title>
<style type="text/css">
div{border: 1px solid red; padding: 20px;}
</style>
</head>
<body>
<div id="divBox">
<input type="button" name="btn1" id="btn1" value="通用跨浏览器点击事件" />
<input type="button" name="btn2" id="btn2" value="事件类型type" />
<input type="button" name="btn3" id="btn3" value="事件目标target" />
<input type="button" name="btn4" id="btn4" value="元素节点名称target.nodeName" />
<input type="button" name="btn5" id="btn5" value="阻止事件冒泡stopPropagation()" />
<a href="http://www.baidu.com" id="Go">打开百度 阻止默认行为preventDefault()</a>
</div>
</body>
<script src="event.js" type="text/JavaScript" charset="utf-8"></script>
<script type="text/javascript">
var Box = document.getElementById("divBox");
EventObject.AddEvent(Box , "click" , mupiao);
function mupiao(){
alert("我是装按扭的DIV")
}
var btn1 = document.getElementById("btn1");
EventObject.AddEvent(btn1 , "click" , mupiao1); //添加事件方法
function mupiao1(){
alert("我调用了封装的事件对象方法!");
}
//EventObject.DelEvent(btn1 , "click" , mupiao); //删除事件方法
var btn2 = document.getElementById("btn2");
EventObject.AddEvent(btn2 , "click" , mupiao2);
function mupiao2(event){
alert(event.type); //事件类型(它是event的属性)
}
var btn3 = document.getElementById("btn3");
EventObject.AddEvent(btn3 , "click" , mupiao3);
function mupiao3(event){
var event = event || window.event;
var ele = event.target || event.srcElement;
alert(ele);//事件目标(它是event的属性)
ele.style.background = "red"
}
var btn4 = document.getElementById("btn4");
EventObject.AddEvent(btn4 , "click" , mupiao4);
function mupiao4(event){
var e = EventObject.GegEvent(event);
var ele = EventObject.GetTarget(e);
alert(ele.nodeName);//元素节点名称(它是event的属性)
}
var btn5 = document.getElementById("btn5");
EventObject.AddEvent(btn5 , "click" , mupiao5);
function mupiao5(event){
event.stopPropagation(); //阻止事件冒泡
alert("阻止事件冒泡stopPropagation()");//元素节点名称(它是event的方法)
}
var go = document.getElementById("go");
EventObject.AddEvent(go , "click" , goFn);
function goFn(event){
EventObject.PreventDefault(event);
EventObject.StopPropagation(event);
alert("阻止默认行为preventDefault()");//元素节点名称(它是event的方法)
}
</script>
</html>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。