jQuery on() stopPropagation 不起作用?

发布于 2024-12-28 21:30:01 字数 577 浏览 3 评论 0原文

我似乎无法让它停止传播..

  $(document).ready(function(){
      $("body").on("click","img.theater",function(event){ 
          event.stopPropagation();    
          $('.theater-wrapper').show();
      }); 

       // This shouldn't fire if I click inside of the div that's inside of the 
       // `.theater-wrapper`, which is called `.theater-container`, anything else it should.
       $(".theater-wrapper").click(function(event){
           $('.theater-wrapper').hide();
       }); 
  });

参考这个 jsfiddle

I can't seem to get this to stop propagating..

  $(document).ready(function(){
      $("body").on("click","img.theater",function(event){ 
          event.stopPropagation();    
          $('.theater-wrapper').show();
      }); 

       // This shouldn't fire if I click inside of the div that's inside of the 
       // `.theater-wrapper`, which is called `.theater-container`, anything else it should.
       $(".theater-wrapper").click(function(event){
           $('.theater-wrapper').hide();
       }); 
  });

Refer this jsfiddle

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

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

发布评论

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

评论(4

打小就很酷 2025-01-04 21:30:01

由于您在 body 元素上使用 on,而不是直接在 img.theater 上使用,因此事件将冒泡到 body< /code> 元素,这就是它的工作原理。

在事件冒泡的过程中 .theater-wrapper 元素 click 事件将被触发,以便您看到它。

如果您不创建任何动态元素,则将点击事件处理程序直接附加到 img.theater 元素上。

$("img.theater").click(function(event){
    event.stopPropagation();    
    $('.theater-wrapper').show();
}); 

或者,您可以检查 .theater-wrapper 元素 click 处理程序内的 click 事件的目标,而不执行任何操作。

$(".theater-wrapper").click(function(event){
    if ($(event.target).is('img.theater')){
         event.stopPropagation();
         return;
    }
    $('.theater-wrapper').hide();
}); 

Since you are using on on the body element and not directly on img.theater the event is going to bubble up to body element and that is how it works.

In the course of event bubbling .theater-wrapper elements click event will be triggered so you are seeing it.

If you are not creating any dynamic elements then attach the click event handler directly on img.theater element.

$("img.theater").click(function(event){
    event.stopPropagation();    
    $('.theater-wrapper').show();
}); 

Alternatively you can check the target of the click event inside .theater-wrapper elements click handler and do nothing.

$(".theater-wrapper").click(function(event){
    if ($(event.target).is('img.theater')){
         event.stopPropagation();
         return;
    }
    $('.theater-wrapper').hide();
}); 
遮云壑 2025-01-04 21:30:01

使用
event.stopImmediatePropagation()
这对我有用

use
event.stopImmediatePropagation()
It worked for me

dawn曙光 2025-01-04 21:30:01

最好的方法是:

$(".theater-wrapper").click(function(event){
    if (!$(event.target).is('img.theater')){
        $('.theater-wrapper').hide();
    }
}); 

它对我来说有手风琴和复选框

The best way to do that is :

$(".theater-wrapper").click(function(event){
    if (!$(event.target).is('img.theater')){
        $('.theater-wrapper').hide();
    }
}); 

It's working for me with an accordion and checkbox

还给你自由 2025-01-04 21:30:01

回应你的 jsfiddle @Dylan

此处:当您单击白点时,它不应关闭。 jsfiddle.net/Cs8Kq - 迪伦

更改

if ($(event.target).is('img.theater')){

if ($(event.target).is('.theater-container')){

,它将起作用。

我通过在事件处理程序中使用 console.log(event.target) 并仅使用当我单击您所说的白色区域时注销的选择器来解决此问题。

我本来会发表评论,但我没有足够的星尘和硬币。

编辑:像这样 jsfiddle.net/heNP4/

In respone to your jsfiddle @Dylan

Here: When you click the white spot it shouldn't close. jsfiddle.net/Cs8Kq - Dylan

Change

if ($(event.target).is('img.theater')){

to

if ($(event.target).is('.theater-container')){

and it will work.

I solved this by using console.log(event.target) in the event handler and just using the selector that got logged out when I clicked the white area you're speaking of.

I would've commented instead, but I don't have enough SO stardust and coins.

EDIT: Like this jsfiddle.net/heNP4/

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