关于javascript冒泡与默认事件的使用详解
时间:2023-05-16 07:16:32
对于javascript的冒泡,我一直误解它了,
冒泡,即是从底层往外blow blow blow ...
惭愧的是,我一直以为阻止冒泡是阻止父元素往子元素传递事件……
贴上一串代码以便往后回顾!
<script type="text/javascript">
window.onload=function(){
var a=document.getElementById("a");
var b=document.getElementById("b");
var c=document.getElementById("c");
var c=document.getElementById("d");
a.onclick=function(e){
this.style.background="#000";
};
b.onclick=function(e){
this.style.background="#ccc";
//阻止事件冒泡
window.event.cancelBubble = true;//IE8以下
e.stopPropagation();
};
d.onmousedown=function(e){
//阻止默认事件,比如在chrome下图片有拖拽默认行为
window.event.returnValue = false;
e.preventDefault();
}
}
</script>
Html部分
<div id="a" style="width:300px;height:300px;background:red;overflow:hidden;">
<div id="b" style="width:200px;height:200px;background:green;margin:50px 0 0 50px;overflow:hidden;">
<div id="c" style="width:100px;height:100px;background:yellow;margin:50px 0 0 50px;overflow:hidden;">
<img src="240x240.jpg" width="50" height="50" id="d" />
</div>
</div>
</div>
另一个例子:
<script type="text/javascript">
window.onload=function(){
document.getElementById("test").addEventListener('click',function(e){
alert('aaaa')
},false);
document.getElementById("test1").addEventListener('click',function(e){
alert('bbb')
e.stopPropagation();
},false)
}
</script>
<style type="text/css">
#test1{width:100px;height:100px;background:#ff0}
#test2{width:100px;height:100px;background:#ff0}
</style>
<div id="test" style="width:100px;height:100px;background:#f60;padding:20px;">
<div id="test1"></div>
</div>
标签:javascript,冒泡,默认事件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java设计模式之装饰模式详解
2022-09-27 05:55:32
![](https://img.aspxhome.com/file/2023/9/78049_0s.png)
Java多线程读写锁ReentrantReadWriteLock类详解
2023-03-09 07:01:54
![](https://img.aspxhome.com/file/2023/3/132203_0s.jpg)
Android中GPS定位的用法实例
2021-07-26 23:26:29
![](https://img.aspxhome.com/file/2023/0/137270_0s.png)
Java毕业设计实战项目之仓库管理系统的实现流程
2021-11-11 09:42:42
![](https://img.aspxhome.com/file/2023/3/74993_0s.jpg)
android 仿微信demo——微信消息界面实现(移动端)
2021-08-01 11:37:26
![](https://img.aspxhome.com/file/2023/2/109172_0s.png)
Flutter 透明状态栏及字体颜色的设置方法
2021-12-09 21:28:30
android 判断横竖屏问题的详解
2022-07-28 08:37:13
面试官:java ThreadLocal真的会造成内存泄露吗
2023-11-18 13:04:56
![](https://img.aspxhome.com/file/2023/7/79937_0s.png)
Android 单双击实现的方法步骤
2023-04-19 02:19:31
百度地图API提示230 错误app scode码校验失败的解决办法
2022-02-17 11:48:10
![](https://img.aspxhome.com/file/2023/3/109173_0s.png)
java常用工具类 Random随机数、MD5加密工具类
2023-02-14 17:55:08
C# 设计模式系列教程-策略模式
2022-04-30 16:09:03
![](https://img.aspxhome.com/file/2023/1/106511_0s.png)
JAVA生成短8位UUID的实例讲解
2021-08-21 04:26:19
C语言中传值与传指针的介绍与区别
2023-08-01 15:26:51
![](https://img.aspxhome.com/file/2023/6/120806_0s.png)
MyBatis使用动态表或列代码解析
2023-06-13 07:57:40
Java中避免过多if-else的几种方法
2023-11-28 13:07:09
Android 之Preference控件基本使用示例详解
2022-10-20 23:42:34
![](https://img.aspxhome.com/file/2023/9/86019_0s.jpg)
微信公众号开发之回复图文消息java代码
2022-11-24 00:56:19
![](https://img.aspxhome.com/file/2023/1/86701_0s.jpg)
JAVA集合框架工具类自定义Collections集合方法
2022-01-11 16:24:13
SpringBoot上传文件并配置本地资源映射来访问文件的实例代码
2023-07-24 02:41:08