简体   繁体   English

为什么我不能停止此js click事件传播

[英]Why can't I stop this js click event propogation

I'm trying to override a click handler by intercepting the event in an inline handler and stopping propagation (propogation disabling code taken from here ). 我试图通过在嵌入式处理程序中拦截事件并停止传播(传播禁用从此处获取的代码)来覆盖点击处理程序。

The following code not only doesn't stop the propagation, it's not even firing the inline handler. 以下代码不仅不会停止传播,甚至不会触发内联处理程序。 I can't tell what's wrong. 我不知道怎么了。

fiddle: http://jsfiddle.net/trKN4/3/ 小提琴: http//jsfiddle.net/trKN4/3/

HTML: HTML:

<a onclick='stopIt(event);'>do it</a>​

JS: JS:

function disableEventPropagation(event)
{
   if (event.stopPropagation){
       event.stopPropagation();
   }
   else if(window.event){
      window.event.cancelBubble=true;
   }
}

function stopIt(event) {
    disableEventPropagation(event);
    alert('dont do it');
}


$(function() {

    $('a').click(function(e){
        alert('im doing it');
    });                   

});

It's not firing the inline function because jsFiddle is wrapping all your code in an onload handler, taking it out of the reachable scope of the handler. 它不会触发内联函数,因为jsFiddle将所有代码包装在onload处理程序中,从而将其移出处理程序的可访问范围。

Choose a no wrap option from the menu on the left. 从左侧菜单中选择“ no wrap选项。

DEMO: http://jsfiddle.net/trKN4/4/ 演示: http : //jsfiddle.net/trKN4/4/

在此处输入图片说明

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

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