jquery live方法,如何禁用一个链接而不是每个链接

时间:2009-10-25 06:42:05

标签: jquery events

如何禁用live('click',function ...)方法然后在ajax调用之后(取决于响应),然后再次启用它?

我读了一些“event.preventDefault();”的地方不要使用实时方法,即使它工作,我怎么能再次启用它?我不能使用die('click',function ...)方法,因为它会禁用所有链接,而不仅仅是我点击的单个链接。

1 个答案:

答案 0 :(得分:3)

假设您有此代码

$('a.clickers').live('click', function() {
    ///...
});

您想要做的是在点击元素时单独输出元素。 jquery中的处理程序函数始终将其上下文设置为它们当时正在处理的元素(即,您单击此==锚点元素)。所以我们需要使用它,并像这样单独调用die。

$('a.clickers').live('click', function clickHandler() {
    // keep a reference to the link that is clicked on so we can refer to it
    // later in the ajax handler.
    var elementClickedOn = this;
    // removes the live event handler
    // from just this link
    $(elementClickedOn).die('click', clickHandler);
    // your code
    // ajax call, im not 100% familiar with ajax in jquery
    // but you get the gist.
    $.ajax(server, function ajaxHandler(responseargs) {
        if (responseargs.reEnableConditionMet) {
            //renable the element's live event handler, by referring to the 
            //original function
            $(elementClickedOn).live('click', clickHandler);
        }
    });
});

我希望这接近你所寻找的。