http://www.php100.com/html/webkaifa/javascript/2009/0418/1637.html
阻止JavaScript事件冒泡传递,阅读阻止JavaScript事件冒泡传 递,CodeJavaScript代码//如果提供了事件对象,则这是一个非IE浏览器if ( e e.stopPropagation )//因此它支持W3C的stopPropagation()方法e.stopPropagation(); else//否则,我们需要使用IE的方式来取
Code
JavaScript代码
//如果提供了事件对象,则这是一个非IE浏览器
if ( e && e.stopPropagation )
//因此它支持W3C的stopPropagation()方法
e.stopPropagation();
else
//否则,我们需要使用IE的方式来取消事件冒泡
window.event.cancelBubble = true;
return false;
2.阻止浏览器的默认行为
JavaScript代码
//如果提供了事件对象,则这是一个非IE浏览器
if ( e && e.preventDefault )
//阻止默认浏览器动作(W3C)
e.preventDefault();
else
//IE中阻止函数器默认动作的方式
window.event.returnValue = false;
return false;
Code
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
2<html xmlns="" lang="gb2312">
3<head>
4<title> 阻止JavaScript事件冒泡传递(cancelBubble 、stopPropagation)</title>
5<meta name="keywords" content="JavaScript,事件冒泡,cancelBubble,stopPropagation" />
6<script type="text/javascript">
7function doSomething (obj,evt) {
8alert(obj.id);
9var e=(evt)?evt:window.event;
10if (window.event) {
11e.cancelBubble=true;
12} else {
13//e.preventDefault();
14e.stopPropagation();
15}
16}
17</script>
18</head>
19<body>
20
21
This is parent1 div.
22
23
This is child1.
24
25
This is parent1 div.
26
27
28
29
This is parent2 div.
30
31
This is child2. Will bubble.
32
33
This is parent2 div.
34
35</body>
36</html>
Comments(0)