如何使用内联 onclick 属性停止事件传播? [英] How to stop event propagation with inline onclick attribute?

查看:31
本文介绍了如何使用内联 onclick 属性停止事件传播?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下事项:

<span onclick="alert('you clicked inside the header');">标题内的内容</span>

我怎样才能让用户点击span时不触发div的点击事件?

解决方案

使用 event.stopPropagation().

<span onclick="event.stopPropagation(); alert('you clicked inside the header');">header里面的东西</span>

对于 IE:window.event.cancelBubble = true

<span onclick="window.event.cancelBubble = true; alert('你点击了标题内部');">标题里面的东西</span>

Consider the following:

<div onclick="alert('you clicked the header')" class="header">
  <span onclick="alert('you clicked inside the header');">something inside the header</span>
</div>

How can I make it so that when the user clicks the span, it does not fire the div's click event?

解决方案

Use event.stopPropagation().

<span onclick="event.stopPropagation(); alert('you clicked inside the header');">something inside the header</span>

For IE: window.event.cancelBubble = true

<span onclick="window.event.cancelBubble = true; alert('you clicked inside the header');">something inside the header</span>

这篇关于如何使用内联 onclick 属性停止事件传播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆