How to stop event propagation with inline onclick attribute?

前端 未结 13 2110
情书的邮戳
情书的邮戳 2020-11-21 23:46

Consider the following:

相关标签:
13条回答
  • 2020-11-22 00:23

    For ASP.NET web pages (not MVC), you can use Sys.UI.DomEvent object as wrapper of native event.

    <div onclick="event.stopPropagation();" ...
    

    or, pass event as a parameter to inner function:

    <div onclick="someFunction(event);" ...
    

    and in someFunction:

    function someFunction(event){
        event.stopPropagation(); // here Sys.UI.DomEvent.stopPropagation() method is used
        // other onclick logic
    }
    
    0 讨论(0)
  • 2020-11-22 00:23

    This also works - In the link HTML use onclick with return like this :

    <a href="mypage.html" onclick="return confirmClick();">Delete</a>
    

    And then the comfirmClick() function should be like:

    function confirmClick() {
        if(confirm("Do you really want to delete this task?")) {
            return true;
        } else {
            return false;
        }
    };
    
    0 讨论(0)
  • 2020-11-22 00:25

    According to this page, in IE you need:

    event.cancelBubble = true

    0 讨论(0)
  • 2020-11-22 00:26

    There are two ways to get the event object from inside a function:

    1. The first argument, in a W3C-compliant browser (Chrome, Firefox, Safari, IE9+)
    2. The window.event object in Internet Explorer (<=8)

    If you need to support legacy browsers that don't follow the W3C recommendations, generally inside a function you would use something like the following:

    function(e) {
      var event = e || window.event;
      [...];
    }
    

    which would check first one, and then the other and store whichever was found inside the event variable. However in an inline event handler there isn't an e object to use. In that case you have to take advantage of the arguments collection which is always available and refers to the complete set of arguments passed to a function:

    onclick="var event = arguments[0] || window.event; [...]"
    

    However, generally speaking you should be avoiding inline event handlers if you need to to anything complicated like stopping propagation. Writing your event handlers separately and the attaching them to elements is a much better idea in the medium and long term, both for readability and maintainability.

    0 讨论(0)
  • 2020-11-22 00:28

    The best solution would be handle with the event through a javascript function, but in order to use a simple and quick solution using the html element directly, and once that the "event" and "window.event" are deprecated and not universally supported (https://developer.mozilla.org/en-US/docs/Web/API/Window/event), I suggest the following "hard code":

    <div onclick="alert('blablabla'); (arguments[0] ? arguments[0].stopPropagation() : false);">...</div>
    
    0 讨论(0)
  • 2020-11-22 00:29

    Keep in mind that window.event is not supported in FireFox, and therefore it must be something along the lines of:

    e.cancelBubble = true
    

    Or, you can use the W3C standard for FireFox:

    e.stopPropagation();
    

    If you want to get fancy, you can do this:

    function myEventHandler(e)
    {
        if (!e)
          e = window.event;
    
        //IE9 & Other Browsers
        if (e.stopPropagation) {
          e.stopPropagation();
        }
        //IE8 and Lower
        else {
          e.cancelBubble = true;
        }
    }
    
    0 讨论(0)
提交回复
热议问题