2017-08-17 62 views
1

有没有办法来防止jQuery .click()监听器对被禁用的按钮的父级的触发? e.g在这种情况下:IE浏览器:jQuery的点击侦听器火灾父母的禁用按钮

<div> 
    <button disabled="disabled">click me</button> 
</div> 
<script> 
    // unfortunately this click handler is placed by 3rd party 
    // library and I can't edit it. Just add another handlers or css 
    $('div').on('click', function() { 
    alert('div is still clickable, probable you use IE'); 
    }); 
</script> 

在我的项目单击事件被置于与第三方库,所以我不能启用/禁用本身或移动结合按钮。

ADD:我发现有没有禁用的按钮,但证明这是一个“已禁用”类,并添加事件侦听器的解决方案:有关禁用的按钮

$('button').on('click', function (event) { 
    if ($(this).hasClass('disabled')) { 
    event.stopImmediatePropagation(); 
    } 
}); 

但什么?有没有办法与它合作?

回答

0

这可能为你工作

$('button').not(':disabled').parent().on('click',function(){}); 

可以去掉第三方点击事件,并重新绑定他们所有的页面上这样也.off()unbind()

$('div').unbind(); //or .off() depending on how the third-party set the handler 
//then add your own click method after the unbinding. 
0

试试这个:

$('div').on('click', function (event) { 
     if ($(this).find('button').is(':disabled')) { 
     return false; 
    } 
    }); 
+0

不幸的是,这个新的事件监听器在调用之后已经是一个所以它不会阻止alert()的调用。 – Tsenzuk