2013-04-11 54 views
0

禁用jQuery的,我有以下的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'); 
    .............. 
} 

所以,當我「登錄」鏈接對話框中單擊上出現,我需要禁用「登錄」鏈接。但即使我更改了類屬性,我也可以點擊並打開另一個登錄對話框。問題是什麼?

+0

使用jQuery.unbind()解除鏈接中的單擊事件:) – 2013-04-11 16:12:58

+0

您的jQuery是否包裝在document.ready調用中? – j08691 2013-04-11 16:13:15

回答

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); 
+0

謝謝,這對我很有用。如果用戶關閉登錄對話框,您能否建議如何恢復此更改?關閉後,我必須重新加載頁面才能夠再次打開它。 – Gayane 2013-04-12 06:08:20

-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'); 
} 
+0

您使用.'on()'會影響每次點擊,並且在第一次點擊之後不會解除綁定。 – j08691 2013-04-11 16:15:08

+0

@ j08691作爲元素將被禁用,它不會有任何區別。 – 2013-04-11 16:16:22

+1

你試過了嗎?我也不記得具有禁用屬性的div。 – j08691 2013-04-11 16:16:41