0

我有以下html代码

<div class='login'><a href='#'>Log in</a> to create custom dashboard</div>

这是我的jQuery代码

$(".login").click(function() {
    $(this).children('a').removeAttr("href");
    $(this).attr('class','disabledlogin');
    ..............
}

因此,当我单击“登录”链接时,会出现对话框,我需要禁用“登录”链接。但即使我更改了类属性,我也可以单击并打开另一个登录对话框。问题是什么?

4

2 回答 2

3

您可以使用.unbind()取消绑定事件处理程序。

$(".login").click(function() {
    $(this).children('a').removeAttr("href");
    $(this).attr('class','disabledlogin');

    // unbind the click handler. it should no longer fire.
    $(this).unbind('click');
});

要恢复,即添加回处理程序,您需要将处理程序分成一个函数并在需要时使用绑定它$(".login").click(onLinkClick);

function onLinkClick() {
    $(this).children('a').removeAttr("href");
    $(this).attr('class','disabledlogin');

    // unbind the click handler. it should no longer fire.
    $(this).unbind('click');
}

// call this initially for binding first time
// call this whenever you need to revert
$(".login").click(onLinkClick); 
于 2013-04-11T16:10:14.043 回答
-2

使用@techfoobar 建议:

$(".login").click(function() {
    $(this).children('a').removeAttr("href");
    $(this).attr('class','disabledlogin');

    // unbind the click handler. it should no longer fire.
    $(this).unbind('click');
}
于 2013-04-11T16:13:53.937 回答