jquery 解除绑定和绑定

发布于 2024-10-10 09:03:17 字数 500 浏览 0 评论 0原文

我只想禁用用户在某些条件下单击某个元素,然后在另一个条件下重新绑定它的功能。以下是我正在使用的一些代码:

 $('#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"); // remove the click for element
    }
    if (current >= 2){
       $("#navigation a:eq(1)").bind("click"); // this doesn't work, but i want re-bind the click here.
    } }

我需要做什么才能使其工作?

I just want to disable the ability for a user to click on an element for some condition and then re-bind it later for another 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;

    if (current == 1){
       $("#navigation a:eq(1)").unbind("click"); // remove the click for element
    }
    if (current >= 2){
       $("#navigation a:eq(1)").bind("click"); // this doesn't work, but i want re-bind the click here.
    } }

What do I need to do to make this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话 2024-10-17 09:03:17

听起来您实际上只想禁用第一个导航链接的工作。如果是这种情况,您只需:

$("#navigation a:first").click(function () { return false; });

因为从事件处理程序返回 false 会阻止浏览器的默认操作(跟随链接)发生。

不过,如果链接不是可点击的,则不要将其设为链接,而是将其变成 :(

var link = $("#navigation a:first");
$("<span>").text(link.text()).attr("class", link.attr("class"))
    .insertBefore(link);
link.remove();

假设链接的 class 属性是唯一有价值的属性复制)。


如果您确实希望取消绑定您编写的自定义处理程序,那么您需要为处理程序函数指定一个名称,以便可以再次引用它以进行重新绑定:

$('#navigation a').bind('click', onClick);

function onClick(e) {
    var $this   = $(this);
    var prev    = current;

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

    if (current == 1){
       $("#navigation a:eq(1)").unbind("click"); // remove the click for element
    } else {
       $("#navigation a:eq(1)").bind("click", onClick); // use the function again
    }
}

It sounds like you actually want to just disable the first navigation link from working. If that's the case, you simply want:

$("#navigation a:first").click(function () { return false; });

as returning false from an event handler prevents the browser's default action (of following the link) from occuring.

Although, if the link is not meant to be clickable, don't make it a link, turn it into a <span>:

var link = $("#navigation a:first");
$("<span>").text(link.text()).attr("class", link.attr("class"))
    .insertBefore(link);
link.remove();

(assuming that the class attribute of the link is the only worthy attribute to copy).


If you actually do wish to unbind the custom handler you wrote, then you want to give the handler function a name so it can be referenced again for rebinding:

$('#navigation a').bind('click', onClick);

function onClick(e) {
    var $this   = $(this);
    var prev    = current;

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

    if (current == 1){
       $("#navigation a:eq(1)").unbind("click"); // remove the click for element
    } else {
       $("#navigation a:eq(1)").bind("click", onClick); // use the function again
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文