取消事件冒泡(多个事件重复发生)

本文介绍了事件冒泡的概念及其实现方式,并提供了一个具体的HTML示例,展示了如何通过JavaScript阻止事件冒泡,这对于理解DOM事件处理至关重要。

 在默认情况下,发生在一个子元素上的单击事件(或者其他事件),如果在其父级元素绑定了一个同样的事件,此时点击子元素,click 事 件会首先被子元素捕获,执行绑定的事件程序,之后会被父级元素捕获,再次激发一段脚本的执行,这就是所谓的“事件冒泡”。

 

 

 

在通常情况下,我们只希望事件发生在它的目标而并非它的父级元素上,只需加个stopBubble 方法,即可取消事件冒泡,见 下:

 

 

现在你可能想知道,什么时候需要阻止事件冒泡?事实上,现在绝大多数情况下都可以不必在意它。但是当你开始开发动态应用程序(尤其是需要处理键盘和 鼠标)的时候,就有这个必要了。

 

 

英文参考:

 

cancelBubble Property



Sets or retrieves whether the current event should bubble up the hierarchy of event handlers.

Syntax

event
.cancelBubble
 [ =
 bCancel
 ]

Possible Values

bCancel Boolean that specifies or receives one of the following values.
false Default. Bubbling is enabled, allowing the next event handler in the hierarchy to receive the event.
true Bubbling is disabled for this event, preventing the next event handler in the hierarchy from receiving the event.

The property is read/write. The property has a default value of false .

Expressions can be used in place of the preceding value(s), as of Microsoft® Internet Explorer 5. For more information, see About Dynamic Properties.

Remarks

Using this property to cancel bubbling for an event does not affect subsequent events.

Example

This example cancels bubbling of the onclick e vent if it occurs in the img object when the user presses the SHIFT key. This prevents the event from bubbling up to the onclick event handler for the document.

 

 

Standards Information

There is no public standard that applies to this property.

<think>嗯,用户问的是阻止事件冒泡和阻止默认事件的区别。我需要先回忆一下这两个概念的具体含义,然后找出它们的区别和应用场景。 首先,事件冒泡是DOM中的事件传播机制,比如点击一个子元素,事件会向上级父元素逐层触发。阻止冒泡就是让事件不再继续上传,比如在子元素处理完事件后,不让父元素接收到这个事件。常用的是event.stopPropagation()方法。 然后是默认事件,指的是浏览器对某些元素的默认行为,比如点击链接会跳转,表单提交会刷新页面。阻止默认事件就是取消这些默认行为,使用event.preventDefault()方法。 需要区分两者的使用场景。比如,如果一个链接绑定了点击事件处理函数,但不想让点击后跳转,就要阻止默认事件。而如果同一个页面上有多个嵌套元素绑定点击事件,可能需要在某个子元素处理完后阻止冒泡,避免父元素的事件处理函数被触发。 可能用户在实际开发中遇到了事件重复触发或者默认行为干扰的问题,需要明确这两个方法的区别。要确保回答结构清晰,用例子说明,避免混淆。 还要注意使用中文,按照系统级指令正确使用LaTeX格式,但这里可能用不上数学表达式。重点在于逻辑清晰,步骤分明,帮助用户理解两者的区别和应用。</think>### 阻止事件冒泡和默认事件的区别 #### 1. **事件冒泡(Event Bubbling)** - **定义**:事件从触发它的最深层元素(目标元素)开始,逐层向上传播到父元素的过程。 - **阻止方法**:调用 `event.stopPropagation()`。 - **作用**:**仅阻止事件向父级传播**,不影响当前元素的事件处理逻辑或默认行为。 - **使用场景**: 例如,当子元素和父元素都绑定了点击事件时,若希望子元素触发后父元素不响应: ```javascript document.getElementById("child").addEventListener("click", function(event) { event.stopPropagation(); // 父元素不会收到此点击事件 console.log("子元素被点击"); }); ``` #### 2. **默认事件(Default Event)** - **定义**:浏览器对某些元素预设的行为(如点击链接跳转、表单提交刷新页面)。 - **阻止方法**:调用 `event.preventDefault()`。 - **作用**:**仅取消浏览器默认行为**,不阻止事件传播。 - **使用场景**: 例如,阻止表单自动提交或禁止链接跳转: ```javascript document.querySelector("a").addEventListener("click", function(event) { event.preventDefault(); // 阻止链接跳转,但事件仍会冒泡到父元素 console.log("链接点击被阻止"); }); ``` --- #### 3. **核心区别** | **特性** | 阻止事件冒泡 (`stopPropagation`) | 阻止默认事件 (`preventDefault`) | |-------------------|---------------------------------------|---------------------------------------| | **影响范围** | 阻止事件向上传播 | 取消浏览器默认行为 | | **事件是否执行** | 当前元素的事件仍会触发 | 当前元素的事件仍会触发 | | **典型用例** | 避免父级元素触发相同事件 | 阻止表单提交、链接跳转等 | --- #### 4. **联动使用示例** ```javascript document.querySelector("button").addEventListener("click", function(event) { event.preventDefault(); // 阻止表单提交(默认行为) event.stopPropagation(); // 阻止事件冒泡到父容器 console.log("按钮点击被处理"); }); ``` - **效果**: - 表单不会提交 - 父容器的点击事件不会被触发 --- #### 5. **总结** - 若需 **隔离事件传播**,用 `event.stopPropagation()` - 若需 **禁用浏览器默认行为**,用 `event.preventDefault()` - 两者可独立使用,也可根据需求组合使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值