如何使用Javascript禁用和重新启用链接?

时间:2015-01-06 14:07:25

标签: javascript html asp.net twitter-bootstrap

有人可以解释我如何禁用链接(仅禁用因为我必须重新启用链接)我已经尝试使用此document.getElementById("aadd").href = "Detail.aspx?using=add";但是当我加载列表元素动态我不能这样做。因为我问你如何禁用链接所以我不能使用它并重新启用它?enter image description here

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>

1 个答案:

答案 0 :(得分:2)

要禁用网页上的所有链接,您可以使用以下代码:

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

重新启用它们:

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

如果您只想禁用特定元素中的链接,请将document.body替换为相应的元素。