简体   繁体   English

如何阻止jQuery中父元素的点击事件?

[英]How to block click event for the parent element in jQuery?

I'm developing a web page using jQuery . 我正在使用jQuery开发网页。 In this web page, there is a div tag that contains a p and a button tag. 在此网页中,有一个div标签,其中包含pbutton标签。

The HTML code is like this: HTML代码是这样的:

<div class="container">
    <div id="attribute" style="border:solid;border-width:2px;border-color:#ccc">
        <p id="cell-content" style="display:inline">Id</p>
        <button id="remark-view" class="btn btn-primary">Detail</button>
    </div>
</div>

and the corresponding JavaScript code is like this: 相应的JavaScript代码是这样的:

$(document).ready(function(){
    $("#attribute").on('click', function(){
        console.log("click on the div attribute");
    });
    $("#attribute").on('dblclick', function(){
        console.log("double click on the div attribute");
    });
    $("#remark-view").on('click', function(){
        console.log("click on the button remark-view");
    });
});

As the code shows, a outer div has ap and button child element, and the outer div element listens on the single click and double click event while the inner button element listens on the single click event. 如代码所示,外部div具有ap和button子元素,外部div元素侦听单击和双击事件,而内部按钮侦听单击事件。

When I run the code in my browser and click on the button, the console shows that both click functions of the outer div and inner button element are called, which is against my purpose: only the click function of inner button should be called at this situation. 当我在浏览器中运行代码并单击按钮时,控制台显示同时调用了外部div和内部按钮元素的click函数,这与我的目的背道而驰:此时仅应调用内部按钮的click函数。情况。 Thus, is there any way to block the click event for the father element(in this case, outer div element).In other words, is there any way to stop passing the click event to the father element after the child element has handled it? 因此,是否有任何方法可以阻止父元素的click事件(在本例中为外部div元素)。换句话说,有任何方法可以在子元素处理完后停止将click事件传递给父元素?

Thank you in advance! 先感谢您!

stopPropagation function will stop the event from bubbling up the DOM. stopPropagation函数将阻止事件使DOM冒泡。

$("#remark-view").on('click', function(event){
        console.log("click on the button remark-view");
        event.stopPropagation()
    });

From the jQuery documentation 来自jQuery文档

Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. 防止事件使DOM树冒泡,从而防止任何父处理程序收到该事件的通知。

This is something I use in one of my sites to do something similar to your problem. 这是我在一个网站中使用的用于执行与您的问题类似的操作的内容。 What the below code does is it prevents the middle div from closing if the button click is on that div . 以下代码的作用是,如果在该div上单击按钮,则会阻止中间div关闭。

//Function for the pop up with the background dimmer
  $(document).mouseup(function(x) {
    var container = $("#divContent"),
       dimmer = $('#bgDimmer');
     if (container.is(":visible")) {
       if (!container.is(x.target) //check if the target of the click isn't the container...
         && container.has(x.target).length === 0) {
          container.hide();
          dimmer.hide();
        }
    }
});

Let try to relate to your code. 让我们尝试与您的代码相关。

//Function for the pop up with the background dimmer
  $(document).mouseup(function(x) {
    var attribute = $('#attribute');
     if (attribute.is(":visible")) {
       if (!attribute.is(x.target) //check if the target of the click isn't the container...
         && attribute.has(x.target).length === 0) {
          console.log("click on the button remark-view");
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM