<!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>
<a href="http://www.baidu.com" id="l">aaa</a>
<a href="http://www.baidu.com" id="l">bbb</a>
<a href="http://www.baidu.com" id="l">ccc</a>
<a href="http://www.baidu.com" id="l">ddd</a>
<a href="http://www.baidu.com" id="l">eee</a>
<a href="http://www.baidu.com" id="l">fff</a>
<div class="">div</div>
<p>ppppppp</p>
<ul>
<li>li1</li>
<li>li2</li>
<li>li3</li>
</ul><script>var N = {}
var links = document.getElementsByTagName('*');//停止时间冒泡
N.stopPropagation = function(e){
e = window.event || e;
if(document.all){
e.cancelBubble = true;
}else{
e.stopPropagation();
}
}// 封装事件函数N.eventHandler = function(node,eventType,handler){
node = typeof node == 'string' ? document.getElementById(node) : node;
if(document.all){
node.attachEvent("on" + eventType,handler)
}else{
node.addEventListener(eventType,handler,false);
}
}N.alertTagName = function(e){
var that = this
var alertTag = function(){
alert(that.tagName)
}
alertTag();
N.stopPropagation(e);
}for(var i=0;i<links.length;i++){
(function(i){
N.eventHandler(links[i],'click',N.alertTagName);
})(i)
}
</script>
</body>
</html>
target = e.target||e.srcElement;
//target就是事件目标。
<!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>
<a href="http://www.baidu.com" id="l">aaa</a>
<a href="http://www.baidu.com" id="l">bbb</a>
<a href="http://www.baidu.com" id="l">ccc</a>
<a href="http://www.baidu.com" id="l">ddd</a>
<a href="http://www.baidu.com" id="l">eee</a>
<a href="http://www.baidu.com" id="l">fff</a>
<div class="">div</div>
<p>ppppppp</p>
<ul>
<li>li1</li>
<li>li2</li>
<li>li3</li>
</ul><script>var N = {}
var links = document.getElementsByTagName('*');//停止时间冒泡
N.stopPropagation = function(e){
e = e||event;
if(document.all){
e.cancelBubble = true;
}else{
e.stopPropagation();
}
}// 封装事件函数N.eventHandler = function(node,eventType,handler){
node = typeof node == 'string' ? document.getElementById(node) : node;
if(document.all){
node.attachEvent("on" + eventType,handler)
}else{
node.addEventListener(eventType,handler,false);
}
}N.alertTagName = function(e){
e = e||event;
var t = e.target||e.srcElement;
var alertTag = function(){
alert(t.tagName)
}
alertTag();
N.stopPropagation(e);
}for(var i=0;i<links.length;i++){
(function(i){
N.eventHandler(links[i],'click',N.alertTagName);
})(i)
}
</script>
</body>
</html>
个人不支持所有事件添加都用 addEventListener 跟 attaEvent。
个人不支持禁止事件冒泡,如果必要,我会用一个布尔值来指示外面的容器不执行冒泡。因为禁止冒泡会导致事件代理失效。