简体   繁体   English

当另一个元素被点击时如何防止div.click事件

[英]How to prevent div.click event when another element clicked

I want to prevent click event but e.stopPropagation() doesn't work. 我想防止点击事件,但e.stopPropagation()不起作用。

I'm trying to write something like Jtable . 我正在尝试编写类似Jtable的内容。 I use div as pagination buttons(1) and another element is delete button. 我使用div作为分页按钮(1),另一个元素是删除按钮。

I need to disable pagination buttons when delete confirmation box is popping up . 当弹出删除确认框时,我需要禁用分页按钮。 After click del or cancel "I want my pagination buttons work again". 单击del后或取消“我希望我的分页按钮再次起作用”。 Is there any solution? 有什么解决办法吗? 在此处输入图片说明

function deleteit() {
  $(".del").click(function() {
    $(".divbutton").click(function(e) {
    alert(1);   
    e.stopPropagation(); 
  });
}

$(".deletebtn").click(function() {
  deleteit();
});

$(".divbutton").click(function() { // I want to prevent this from click
  //ajax send and display data
});

I know that I should use input disabled instead of div, but I need to use div. 我知道我应该使用输入禁用而不是div,但是我需要使用div。 edit:add image to make it understandable. 编辑:添加图像使其易于理解。

Instead of preventing the click handler... remove the registered handler using .off() , here namespaced event name is used because we want to remove only a very specific handler 而不是阻止单击处理程序...使用.off()删除注册的处理程序,这里使用命名空间事件名称是因为我们只想删除非常特定的处理程序

function deleteit() {
    $(".del").click(function () {
        $(".divbutton").off('click.delete')
    })
}

$(".deletebtn").click(function () {
    deleteit();
});

$(".divbutton").on('click.delete', function () { // I want to prevent this from click
    //ajax send and display data
});

Why your code is not working? 为什么您的代码无法正常工作? because stopping propagation will prevent the bubbling up of the event but in your case both the event are registered to the divbutton so both of them will get triggered even if propagation is prevented. 因为停止传播将防止事件冒泡,但是在您的情况下,两个事件都已注册到divbutton因此即使阻止了传播,两个事件也会被触发。

Another way is to use stopImmediatePropagation() even that is not possible here because your delete registers the handler later and it will get executed only after the first one is called 另一种方法是使用stopImmediatePropagation(),即使在这里也不可行,因为您的delete稍后会注册处理程序,并且只有在调用第一个处理程序后才会执行

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

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