Button inside of div, not to send div onclick - JavaScript

后端 未结 1 970
情书的邮戳
情书的邮戳 2021-01-14 05:50

I have a div, and that div has an onclick event. Inside of the div, there is some text, and a button. When I click on the text, the div onclick is sent, which is good. Howev

相关标签:
1条回答
  • 2021-01-14 06:34

    Use stopPropagation event function. This way divClick function will not be called.

    function buttonClick(e) {
       if (!e) e = window.event;
       e.stopPropagation();
       // do what you want
    }
    

    Because of Firefox you need to explicitly send event as argument like this:

    <button name="test" onclick="buttonClick(event)">Click Me</button>
    

    Demo

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