[英]IE: jQuery click listener fires on parent of disabled button
有没有一种方法可以防止在禁用的按钮的父级上触发jQuery .click()侦听器? 例如在这种情况下:
<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>
在我的项目中,click事件放置在第三方库中,因此我无法启用/禁用自身或将绑定移动到按钮。
添加 :我找到了不禁用按钮,而是将其证明为“禁用”类,并添加事件侦听器的解决方案:
$('button').on('click', function (event) {
if ($(this).hasClass('disabled')) {
event.stopImmediatePropagation();
}
});
但是禁用按钮呢? 有没有办法使用它?
这可能对你有用
$('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.
尝试这个:
$('div').on('click', function (event) {
if ($(this).find('button').is(':disabled')) {
return false;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.