啟用/禁用 a元素

來源:互聯網
上載者:User

在Asp.net中,有時候需要禁用掉一個a連結元素.

在伺服器端,比較容易;只需要設定LinkButton的Enabled屬性即可.

 

那麼在用戶端,如何使用javascript來實現呢? 如果你想當然的認為,使用a.disabled=true就能實現,那就錯了.

正確的方法是:同時設定disabled屬性和href屬性:

 1)禁用a元素;

   

 // 功能: 禁用一個a元素; // 參數: //     link: a元素對象; function disableLink(link) {     //設定href屬性     link.href = "javascript:void(0);";     //設定disabled屬性     link.setAttribute("disabled", "disabled"); }
 2)啟用a元素;
 // 功能: 啟用一個a元素;  // 參數:  //     link: a元素對象;  function enableLink(link) {       // 將id中的所有底線(_)全部替換為美元 ($)符號;       var postbackID = link.id.replace(/_/gi, "{1}quot;);       // 重新設定href       link.href = "javascript:__doPostBack('" + postbackID + "','')";       // 刪除disabled屬性      link.removeAttribute("disabled");  }

注意:LinkButton在的用戶端ID屬性預設使用底線(_)分隔字元,在伺服器端則預設使用美元($)分隔字元;所以調用__doPostBack()方法時,需要將它的id屬性轉換一下,否則不會觸發相關的伺服器端事件.

在w3c標準中,HTMLAnchorElement是不包括disabled屬性的(可參照:http://www.w3.org/TR/2010/WD-html5-20100304/text-level-semantics.html#the-a-element)

但是HTMLElement都可以設定disabled屬性(通過setAttribute設定自訂屬性),詳細請參照(http://www.w3schools.com/jsref/dom_obj_all.asp);
你可以通過設定LinkButton.Enabled = false,然後查看HTML代碼就可能看到產生的a元素包含一個屬性:disabled="disabled".  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.