2015-01-06 62 views
-1

有人可以解釋我如何禁用鏈接(只禁用,因爲我必須重新啓用鏈接)我已嘗試使用此document.getElementById("aadd").href = "Detail.aspx?using=add";,但是當我加載列表元素動態我不能這樣做。因爲我問你如何禁用鏈接,所以我不能使用它並重新啓用它? enter image description here如何禁用並重新啓用JavaScript鏈接?

function changebuttonstate(thisbtn) { 

     if (document.getElementById("add").disabled == true) { 
      document.getElementById("add").disabled = false; 
      document.getElementById("aadd").href = "Detail.aspx?using=add"; 
      var elems = document.getElementsByClassName("disabledbutton"); 
      for (var i = 0; i < elems.length; i++) { 
       elems[i].disabled = false; 
      } 

     } else { 
      document.getElementById("add").disabled = true; 
      document.getElementById("aadd").href = "#"; 
      var elems = document.getElementsByClassName("disabledbutton"); 
      for (var i = 0; i < elems.length; i++) { 
       elems[i].disabled = true; 
      } 
      thisbtn.disabled = false; 
     } 

     if (document.getElementById("refresh").disabled == true) { 
      document.getElementById("refresh").disabled = false; 
      document.getElementById("arefresh").href = ""; 
      var elems = document.getElementsByClassName("disabledbutton"); 
      for (var i = 0; i < elems.length; i++) { 
       elems[i].disabled = false; 
      } 
     } else { 
      document.getElementById("refresh").disabled = true; 
      document.getElementById("arefresh").href = "#"; 
      var elems = document.getElementsByClassName("disabledbutton"); 
      for (var i = 0; i < elems.length; i++) { 
       elems[i].disabled = true; 
      } 
      thisbtn.disabled = false; 

     } 
    } 

<a href="Detail.aspx?using=edit&id={{x.ID}}"><input type="button" id="Edit" value="Edit" class="btn btn-primary disabledbutton"/></a> 
       <button type="button" class="btn btn-danger disabledbutton" data-toggle="collapse" data-target="#demo{{x.ID}}" onclick="changebuttonstate(this)">Delete</button> 
       <div id="demo{{x.ID}}" class="collapse disabledbutton"> 
        <div class="well"> 
        <p>Are you sure?</p> 
         <a href="Delete.aspx?id={{x.ID}}"><input type="button" id="Button1" class="btn btn-danger" value="Delete"/></a> 
        </div> 
       </div> 

<a href="Detail.aspx?using=add" id="aadd"><input type="button" id="add" value="Add" class="btn btn-success"/></a> 
     <a href="listview.aspx" id="arefresh"><input type="button" id="refresh" value="Refresh" class="btn btn-primary"/></a> 
+3

附註:我可以看到你在'a'內有一個'input',這是無效的。 – melancia

+0

不,它不工作 –

+3

我沒有說它不會工作。我說這是無效的標記。嵌套兩個可點擊元素有什麼意義?你點擊一個,你點擊了兩個。 – melancia

回答

1

要禁用頁面上的所有鏈接,您可以使用此代碼;

document.body.addEventListener("click", function stopLinks(e){ 
    if (this.nodeName==="A"){ 
     e.preventDefault(); 
     return false; 
    } 
}) 

,並重新啓用

document.body.removeEventListenr("click", stopLinks); 

,如果你只是想禁用特定元素中的鏈接,使用適當的元素取代document.body

相關問題