Javascript jquery - 禁用点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4589964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:07:17  来源:igfitidea点击:

jquery - disable click

javascriptjquery

提问by scifirocket

I just want to disable the ability for a user to click on an element for some condition. Here is some of the code I am working with:

我只想禁用用户在某些条件下单击元素的能力。这是我正在使用的一些代码:

     $('#navigation a').bind('click',function(e){

    var $this   = $(this);
    var prev    = current;

    current = $this.parent().index() + 1; // store the position in current

    if (current == 1){
    $("#navigation a:eq(1)").unbind("click"); // i want to disable the ability to click this element if current is 1
    }
    if (current >= 2){
    $("#navigation a:eq(1)").bind("click"); // this is wrong, but I want to rebind the click if current is greater than 1.  
    }

}

}

回答by zzzzBov

If you're using jQuery versions 1.4.3+:

如果您使用的是 jQuery 1.4.3+ 版本:

$('selector').click(false);

If not:

如果不:

$('selector').click(function(){return false;});

回答by ehudokai

assuming your using click events, just unbind that one.

假设您使用点击事件,只需解除绑定即可。

example

例子

if (current = 1){ 
    $('li:eq(2)').unbind("click");
}

EDIT: Are you currently binding a click event to your list somewhere? Based on your comment above, I'm wondering if this is really what you're doing? How are you enabling the click? Is it just an anchor(<a>tag) ? A little more explicit information will help us answer your question.

编辑:您当前是否将点击事件绑定到您的列表中的某处?根据您上面的评论,我想知道这是否真的是您在做什么?你是如何启用点击的?它只是一个锚(<a>标签)吗?更明确的信息将有助于我们回答您的问题。

UPDATE:

更新:

Did some playing around with the :eq() operator. http://jsfiddle.net/ehudokai/VRGfS/5/

使用 :eq() 运算符进行了一些操作。http://jsfiddle.net/ehudokai/VRGfS/5/

As I should have expected it is a 0 based index operator. So if you want to turn of the second element in your selection, where your selection is

正如我所料,它是一个基于 0 的索引运算符。因此,如果您想关闭选择中的第二个元素,您的选择所在的位置

$("#navigation a")

you would simply add :eq(1) (the second index) and then .unbind("click") So:

您只需添加 :eq(1) (第二个索引)然后 .unbind("click") 所以:

if(current == 1){
    $("#navigation a:eq(1)").unbind("click");
}

Ought to do the trick.

应该做的伎俩。

Hope this helps!

希望这可以帮助!

回答by Ben

Raw Javascript can accomplish the same thing pretty quickly also:

原始 Javascript 也可以很快完成同样的事情:

document.getElementById("myElement").onclick = function() { return false; } 

回答by Joko Wandiro

You can use unbind method to remove handler that has been attached...

您可以使用 unbind 方法删除已附加的处理程序...

if (current = 1){ 
   $('li:eq(2)').unbind('click');
}

You can check what can unbind do ? Unbind manual

你可以检查 unbind 可以做什么?解绑手册

回答by Sajitha Rathnayake

Use off()method after click event is triggered to disable element for the further click.

off()触发单击事件后使用方法禁用元素以供进一步单击。

$('#clickElement').off('click');

回答by E Work You

/** eworkyou **//

/** 电子工作你 **//

$('#navigation a').bind('click',function(e){

    var $this   = $(this);
    var prev    = current;

    current = $this.parent().index() + 1; //

    if (current == 1){
    $("#navigation a:eq(1)").unbind("click"); // 
    }
    if (current >= 2){
    $("#navigation a:eq(1)").bind("click"); // 
    }