咿呀咿呀哒哒
2016-08-24 11:19
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="event.js"></script>
<script src="javascript.js"></script>
</head>
<body>
<div id="box">
<input type="button" id="btn1" value="按钮1"/>
<input type="button" id="btn2" value="按钮2"/>
<a href="http://www.baidu.com/" id="go" >跳转</a>
</div>
</body>
</html>
//event.js
var eventUtil={
addHandler:function(element,type,handler){
if(element.addEventListener){
element.addEventListener(type,handler,false);
}
else if(element.attachEvent){
element.attachEvent('on'+type,handler);
}
else{
element['on'+type]=handler;
}
}
removeHandler:function(element,type,handler){
if(element.removeEventListener){
element.removeEventListener(type,handler,false);
}
else if(element.detachEvent){
element.detachEvent('on'+type,handler);
}
else{
element['on'+type]=null;
}
}
getEvent:function(event){
return event?event:window.event;
//return event=event||event=window.event;
}
getEventType:function(event){
return event.type;
}
getEventsrc:function(event){
return event.target || event.srcElement;
}
//阻止事件冒泡
stopBubble:function(event){
if(event.stopPropagation){
event.stopPropagation();
}
else{
event.cancelBubble=true;
}
}
//组织事件默认行为
stopaction:function(event){
if(event.preventDefault){
event.paeventDefault();
}
else{
event.returnValue=false;
}
}
}
// JavaScript Document
window.onload=function(){
var go=document.getElementById('go'),
box=document.getElementById('box');
eventUtil.addHandler(box,'click',function(){
alert('我是一个复合纸');
}
);
}
我把代码拷贝执行,能正常跳转,就是复合纸那个文本信息没有显示,可以是浏览器的问题
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>无标题文档</title>
</head>
<body>
<div id="box">
<input type="button" id="btn1" value="按钮1"/>
<input type="button" id="btn2" value="按钮2"/>
<a href="http://www.baidu.com/" id="go" >跳转</a>
</div>
<script>
//event.js
var eventUtil={
addHandler:function(element,type,handler){
if(element.addEventListener){
element.addEventListener(type,handler,false);
}
else if(element.attachEvent){
element.attachEvent('on'+type,handler);
}
else{
element['on'+type]=handler;
}
}
/* 把其余方法注释掉就能运行出来了 */
/* removeHandler:function(element,type,handler){
if(element.removeEventListener){
element.removeEventListener(type,handler,false);
}
else if(element.detachEvent){
element.detachEvent('on'+type,handler);
}
else{
element['on'+type]=null;
}
} */
/* getEvent:function(event){
return event?event:window.event;
//return event=event||event=window.event;
}
getEventType:function(event){
return event.type;
}
getEventsrc:function(event){
return event.target || event.srcElement;
} */
//阻止事件冒泡
/* stopBubble:function(event){
if(event.stopPropagation){
event.stopPropagation();
}
else{
event.cancelBubble=true;
}
} */
//组织事件默认行为
/* stopaction:function(event){
if(event.preventDefault){
event.paeventDefault();
}
else{
event.returnValue=false;
}
} */
}
// JavaScript Document
window.onload=function(){
var go=document.getElementById('go'),
box=document.getElementById('box');
eventUtil.addHandler(box,'click',function(){
alert('我是一个复合纸');
}
);
}
</script>
</body>
</html>
DOM事件探秘
99544 学习 · 1197 问题
相似问题