单击超链接时阻止触发父容器单击事件 [英] Prevent parent container click event from firing when hyperlink clicked

查看:115
本文介绍了单击超链接时阻止触发父容器单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网页上,我有一个文件列表。

On my web page I have a list of files.

每个文件都在它自己的容器div中(div class =file)。容器内部是文件和描述的链接。

Each file is in it's own container div (div class="file"). Inside the container is a link to the file and a description.

我想允许用户点击容器上的任意位置下载文件。我这样做是通过向容器添加一个click事件并检索子链接的href。

I wanted to allow a user to click anywhere on the container to download the file. I did this by adding a click event to the container and retrieving the href of the child link.

当文件在新窗口中打开时,如果用户实际点击了链接,该文件打开两次。

As the file opens in a new window, if the user actually clicks the link, the file opens twice.

因此,我需要防止在单击超链接时触发父容器单击事件。这样做的最佳方法是将超链接功能添加到超链接to0并设置event.stopPropagation吗?据推测,这会阻止事件冒泡到集装箱。

So I need to prevent the parent container click event from firing when the hyperlink is clicked. Would the best way of doing this be to add a click function to the hyperlink to0 and set event.stopPropagation? Presumably this would then stop the event from bubbling up to the container.

谢谢
Ben

Thanks Ben

推荐答案

是的,使用stopPropagation。请参阅:防止执行父事件处理程序

Yes, use stopPropagation. See: Prevent execution of parent event handler

这篇关于单击超链接时阻止触发父容器单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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