当前位置:首页 > H5 > 正文内容

Javascript事件冒泡和捕捉的阻止

高老师9年前 (2017-07-02)H51921
<!DOCTYPE html>
<html>
<head>
<title>捕捉和冒泡</title>
<meta charset="utf-8">
</head>
<body>
<style>
 
#china{
width:500px;
height: 500px;
}
div{
border: 1px solid #333;
width:80%;
height: 80%;
margin: 10%;
}
 
</style>
 
 
<div id="china">
我是中国区域
<div id="shanxi">
 我是陕西区域
<div id="xian">
我是西安区域
 
 
</div>
</div>
</div>
 
 
<script>
//获取中国,陕西,西安三大对象
var china =document.getElementById('china');
var shanxi=document.getElementById('shanxi'); 
var xian  =document.getElementById('xian'); 
 
//第三个参数,是否捕捉,默认值是false,不捕捉即默认为冒泡【目前是捕捉模式】
// china.addEventListener('click',function(){console.log('China')},true);
// shanxi.addEventListener('click',function(){console.log('Shanxi')},true); 
// xian.addEventListener('click',function(){console.log('Xian')},true); 
 
 
 
//第三个参数,是否捕捉,默认值是false,不捕捉即默认为冒泡【目前是冒泡模式】
china.addEventListener('click',function(){console.log('China')},false);
shanxi.addEventListener('click',function(ev){
console.log('Shanxi');
ev.stopPropagation();
},false);
xian.addEventListener('click',function(){console.log('Xian')},false); 
 
 
</script>
 
 
 
</body>
</html>

【主要方法】:stopPropagation()

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/244.html

分享给朋友:

“Javascript事件冒泡和捕捉的阻止” 的相关文章

websql的使用方法

websql的使用方法

<!--          三个核心方法          openDatabase:这个方法使用现有数据库或创建...

Js显示本周星期对应的日期,非固定,周一,周三,周六

Js显示本周星期对应的日期,非固定,周一,周三,周六

开发需求如下,商品下单后需要指定送到日期:送达日期为周一 周三  周六 ,如果今天周一,用户选择周一即为下一周周一。代码如下:<html> <head> <meta charset="utf-8"> <title>...

Javascript页面播放语音,Javascript语音读取页面的文字

Javascript页面播放语音,Javascript语音读取页面的文字

<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script type...

javascript使用原型为内置对象增加方法

javascript使用原型为内置对象增加方法

//字面量的形式创建原型对象 /* function Box(){} Box.prototype={ 'name':'gao', age:23, fun:function(){ return this.name+'--'+...

js 点击复制,js实现手机端点击复制,js实现点击复制,js点击复制到剪贴板

js 点击复制,js实现手机端点击复制,js实现点击复制,js点击复制到剪贴板

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>点击复制</title> </head>...

rem布局原理,rem自适应布局,rem布局快速方案

rem布局原理,rem自适应布局,rem布局快速方案

rem原理和简介(1).字体单位单位的值是根据html根元素大小而定,同样可以作为宽度高度单位(2).适配原理将px替换为rem,动态修改html的font-size适配(3).单位1rem=16px,why? F12中1rem在computed中font-size属性中会显示为font-size:...