2009-12-17 28 views
4

有誰能如何使用javascript禁用一個標籤(鏈接)?使用javascript禁用了一個標籤(鏈接)

例子:

<div class="sub-heading"> 
    Contact Details &nbsp; &nbsp; &nbsp; 
    <a href="./cust_add_edit_customer.php?action=edit_customer_details&amp;cust_code=12761"> 
    <img class="imgVA editIconPad" src="../images/edit0.gif" 
     alt="Edit Contact Details" border="0" width="20" height="17"> 
    </a> 
</div> 

我希望這個殘疾人被點擊一個按鈕後的標籤。

+0

一個格或錨標記 – 2009-12-17 04:23:07

+0

您要禁用它或者把它隱藏 – 2009-12-17 04:23:54

+0

什麼做哪些你的意思是「禁用」?我假設你不希望用戶能夠點擊它並導航到它的目標。你是否還需要防止右鍵單擊「在新窗口/選項卡中打開」? – Annabelle 2009-12-17 04:25:00

回答

4

a標記上使用onclick="this.onclick=function(){return false}"屬性。如果有很多按鈕,則應該在JavaScript腳本中迭代它們,從而爲click添加一個返回false的函數的事件偵聽器。

2

function disableAnchor(obj, disable) 
{ 
    if (disable) { 
     var href = obj.getAttribute("href"); 
     if (href && href != "" && href != null) { 
      obj.setAttribute('href_bak', href); 
     } 
     obj.removeAttribute('href'); 
     obj.style.color="gray"; 
    } 
    else { 
     obj.setAttribute('href', obj.attributes['href_bak'].nodeValue); 
     obj.style.color="blue"; 
    } 
} 

var Link_Enabled_Flag = false; // disable links - background process changes this to true when it's done 

function Check_Link_Enabled(){ return Link_Enabled_Flag; } 

<a href="wherever.com" onclick="return Check_Link_Enabled()"></a> 

IE和Firefox兼容JavaScript來啓用或禁用一個錨標記

onclick="disableAnchor(this,'verify')" 

function disableAnchor(Check_Obj, Check_Id){ 
var Anchor_id = 's'; 
thisCheckbox = document.getElementById(Check_Id); 
thisAnchor = document.getElementById(Anchor_id); 
if(thisCheckbox.checked){ 
//alert('Y1'); 
Check_Obj.setAttribute('href',''); //Check_Obj.attributes['href_bak'].nodeValue 
Check_Obj.style.color="blue"; 
//alert('Y2'); 
} 
else{ 
    //alert('N1');  

var href = Check_Obj.getAttribute('href'); 
//alert(href); 
if(href && href != "" && href != null){ 
Check_Obj.setAttribute('href_bak', href); 
} 
Check_Obj.removeAttribute('href'); 
Check_Obj.style.color="gray"; 

//alert('N2'); 
    } 
} 
3

添加id屬性您要禁用a標籤,然後:

document.getElementById('the_id').href = '#'; 
+2

它仍然是可點擊的,並將頁面滾動到頂部。 – rahul 2009-12-17 04:27:15

2

您可以使用jQuery

$('sub-heading').attr("disabled", "true"); 
+0

選擇器不正確。對於示例HTML,它應該是'$('。sub-heading a')。attr(「disabled」,「true」)' – Jacob 2009-12-17 04:37:25

+5

另外,disabled不是''元素的屬性。 – Jacob 2009-12-17 04:42:08

5

我覺得最人性化的做法是隱藏的鏈接。在您的按鈕單擊處理程序做:

document.getElementById('anchorID').style.visibility = 'hidden'; 

然後重新啓用它:

document.getElementById('anchorID').style.visibility = 'visible'; 
0

你可以這樣說:

<a href="javascript:if (links_enabled) document.location='www.example.com';">enabled or disabled link</a> 
<br/> 
<a href="javascript:links_enabled = true;">enable link</a> 
<br/> 
<a href="javascript:links_enabled = !links_enabled;">toggle link</a> 

我覺得這是非常優雅,而且它也將使鏈接僅工作javascript已啓用。換句話說,鏈接默認是禁用的。

1

刪除屬性href的:

<a>Edit</a> 
0

你也可以做到這一點的方式...

<a style="cursor:Default; text-decoration:none; color:inherit;">.....</a>