jQuery 点击事件停止传播无法工作?

发布于 2024-12-28 18:57:51 字数 661 浏览 2 评论 0原文

我有一个画廊类型的界面,我试图开始工作,我希望能够单击它的外部来关闭它,但是里面有一个 div,其中包含主要元素、照片和要单击的内容。然而,就像现在一样,当您在 div 内部单击时,它会关闭,因为它是元素中的子元素,当您单击时,它会关闭。

我有这样的 div:

 <div class="theater-wrapper">
     <div class="theater-container"></div>
 </div>

所有内容都通过 ajax 加载到剧院容器中。 当您单击 .theater-wrapper 时,它应该触发事件关闭,但是当您单击 Theater-container 时,它不应该触发该事件。

这就是我尝试关闭它的方式:

$(".theater-wrapper").click(function (event) {
     $('.theater-wrapper').hide(); 
event.stopPropagation();

  });

我有一个 jsfiddle 显示此操作: http://jsfiddle.net /Cs8Kq/1/

I have a gallery type of interface that I'm trying to get to work, I want to be able to click outside of it to close it, however there is a div inside that contains the main elements, photos, and things to click. However as it is now when you click inside the div it closes, because it's a child in the element that when you click it closes.

I have the divs like this:

 <div class="theater-wrapper">
     <div class="theater-container"></div>
 </div>

everything is loaded into theater-container via ajax.
When you click .theater-wrapper it should fire the event to close, however when you click theater-container it shouldn't.

This is how I have tried to close it:

$(".theater-wrapper").click(function (event) {
     $('.theater-wrapper').hide(); 
event.stopPropagation();

  });

I have a jsfiddle showing this in action: http://jsfiddle.net/Cs8Kq/1/

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

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

发布评论

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

评论(1

别在捏我脸啦 2025-01-04 18:57:51

如果您想停止在 .theater-container 上传播点击事件,那么您需要在其中放置命令。现在您已将其应用于 .theater-wrapper 点击操作。

$(".theater-container").click(function (ev) {
    ev.stopPropagation();
});

If you want to stop propagation of the click event on .theater-container, then that's where you need to put the command. Right now you have it applied to the .theater-wrapper click action.

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