stopPropagation() 没有停止事件冒泡?

发布于 2024-09-25 19:23:23 字数 363 浏览 0 评论 0原文

jsFiddle

我正在使用一个 jQuery 插件,它允许用户在一个区域中绘制方框。我在用户松开鼠标按钮时出现的框中放置了一个下拉列表。问题是,当您尝试单击下拉列表来选择一个选项时,会绘制另一个框。我尝试使用以下代码来停止点击事件冒泡(这位于 jsFiddle 中 javascript 文件的底部附近),但它不起作用:

$('.dropdown').click( function(e) {
    e.stopPropagation();
    return true;
});

我做错了什么?感谢您的阅读。

jsFiddle

I'm using a jQuery plugin that allows the user to draw boxes in an area. I've put a dropdown list in the box that appears when the users lets go of the mouse button. The problem is, when you try to click on the dropdown list to select an option, another box is drawn. I've tried to use the following code to stop the click event bubbling (this is near the bottom of the javascript file in the jsFiddle), but it doesn't work:

$('.dropdown').click( function(e) {
    e.stopPropagation();
    return true;
});

What am I doing wrong? Thanks for reading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别挽留 2024-10-02 19:23:23

解决方法是检查鼠标拖动开始时的目标是什么。像这样的事情,

if (event.target.tagName.toLowerCase() != 'div' ) { return; }

或者说,在所有发生的事件中。

OP 演示,已更新

A workaround is to check what is the target when mouse drag start. Something like this,

if (event.target.tagName.toLowerCase() != 'div' ) { return; }

or, in all events that happened.

OP's demo, updated

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文