2012-06-12 71 views
2

我有一個圖像與錨標記相關聯,一旦用戶點擊圖像彈出加載。我想禁用此錨標記。使用jquery禁用錨標記

的HTML代碼看起來像:

<a href="#" class="openModalLink"> 
<img style="vertical-align: middle; border: none" width="9%" alt="" id="imgmap" class="zoom" /></a> 

我曾嘗試下面的代碼,但似乎並沒有工作

$(".openModalLink").off("click"); 
$(".openModalLink").attr("disabled", true); 
$(".openModalLink").attr("disabled", "disabled"); 

感謝您的答覆

+1

:)這個確切的問題是問了很多次bruv移除事件處理程序:看到重複:http://stackoverflow.com/ questions/1164635/how-to-enable-or-disable-an-anchor-using-jquery –

回答

14

你可以這樣做

$('.openModalLink').click(function(event){ 
    event.preventDefault(); 
}); 

也可參考docs

編輯:

要啓用和禁用錨標籤

function disabler(event) { 
    event.preventDefault(); 
    return false; 
} 

$('#enable').click(function(){ 
    $('.openModalLink').unbind('click',disabler); 
}); 
$('#disable').click(function(){ 
    $('.openModalLink').bind('click',disabler); 
}); 
​ 

DEMO

編輯2:

在jQuery 1.7 .on().off()優於綁定和取消綁定附加和元素

$('#enable').click(function() { 
    $('body').off('click', '.openModalLink', disabler); 
}); 
$('#disable').click(function() { 
    $('body').on('click', '.openModalLink', disabler); 
});​ 
+0

謝謝,以及如何啓用它。 – Prince

+0

已更新答案和示例。 – Dhiraj