How do I prevent a parent's onclick event from firing when a child anchor is clicked?

后端 未结 22 1403
旧巷少年郎
旧巷少年郎 2020-11-22 00:37

I\'m currently using jQuery to make a div clickable and in this div I also have anchors. The problem I\'m running into is that when I click on an anchor both click events ar

相关标签:
22条回答
  • 2020-11-22 01:04

    Here's an example using Angular 2+

    For example, if you wanted to close a Modal Component if the user clicks outside of it:

    // Close the modal if the document is clicked.
    
    @HostListener('document:click', ['$event'])
    public onDocumentClick(event: MouseEvent): void {
      this.closeModal();
    }
    
    // Don't close the modal if the modal itself is clicked.
    
    @HostListener('click', ['$event'])
    public onClick(event: MouseEvent): void {
      event.stopPropagation();
    }
    
    0 讨论(0)
  • 2020-11-22 01:04

    To specify some sub element as unclickable write the css hierarchy as in the example below.

    In this example I stop propagation to any elements (*) inside td inside tr inside a table with the class ".subtable"

    $(document).ready(function()
    {    
       $(".subtable tr td *").click(function (event)
       {
           event.stopPropagation();
       });
    
    });
    
    0 讨论(0)
  • 2020-11-22 01:04
    <a onclick="return false;" href="http://foo.com">I want to ignore my parent's onclick event.</a>
    
    0 讨论(0)
  • 2020-11-22 01:06

    var inner = document.querySelector("#inner");
    var outer = document.querySelector("#outer");
    inner.addEventListener('click',innerFunction);
    outer.addEventListener('click',outerFunction);
    
    function innerFunction(event){
      event.stopPropagation();
      console.log("Inner Functiuon");
    }
    
    function outerFunction(event){
      console.log("Outer Functiuon");
    }
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>Pramod Kharade-Event with Outer and Inner Progration</title>
    </head>
    <body>
    <div id="outer" style="width:100px;height:100px;background-color:green;">
      <div id="inner" style="width:35px;height:35px;background-color:yellow;"></div>
    </div>
    </body>
    </html>

    0 讨论(0)
  • 2020-11-22 01:09

    All solution are complicated and of jscript. Here is the simplest version:

    var IsChildWindow=false;
    
    function ParentClick()
    {
        if(IsChildWindow==true)
        {
            IsChildWindow==false;
            return;
        }
        //do ur work here   
    }
    
    
    function ChildClick()
    {
        IsChildWindow=true;
        //Do ur work here    
    }
    
    0 讨论(0)
  • 2020-11-22 01:10

    In case someone had this issue using React, this is how I solved it.

    scss:

    #loginBackdrop {
    position: absolute;
    width: 100% !important;
    height: 100% !important;
    top:0px;
    left:0px;
    z-index: 9; }
    
    #loginFrame {
    width: $iFrameWidth;
    height: $iFrameHeight;
    background-color: $mainColor;
    position: fixed;
    z-index: 10;
    top: 50%;
    left: 50%;
    margin-top: calc(-1 * #{$iFrameHeight} / 2);
    margin-left: calc(-1 * #{$iFrameWidth} / 2);
    border: solid 1px grey;
    border-radius: 20px;
    box-shadow: 0px 0px 90px #545454; }
    

    Component's render():

    render() {
        ...
        return (
            <div id='loginBackdrop' onClick={this.props.closeLogin}>
                <div id='loginFrame' onClick={(e)=>{e.preventDefault();e.stopPropagation()}}>
                 ... [modal content] ...
                </div>
            </div>
        )
    }
    

    By a adding an onClick function for the child modal (content div) mouse click events are prevented to reach the 'closeLogin' function of the parent element.

    This did the trick for me and I was able to create a modal effect with 2 simple divs.

    0 讨论(0)
提交回复
热议问题