如何使用 Javascript 禁用和 re-enable 一个 link?
How to disable and re-enable an link with Javascript?
谁能解释一下我如何禁用 link(仅禁用是因为我必须重新启用 link)我曾尝试使用此 document.getElementById("aadd").href = "Detail.aspx?using=add";
但是当我加载列表时元素动态我不能这样做。因为我问你如何禁用 link 所以我不能使用它并重新启用它?
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>
要禁用页面上的所有链接,您可以使用此代码:
document.body.addEventListener("click", function stopLinks(e){
if (this.nodeName==="A"){
e.preventDefault();
return false;
}
})
并重新启用它们:
document.body.removeEventListener("click", stopLinks);
如果您只想禁用特定元素内的链接,请将 document.body
替换为适当的元素。
谁能解释一下我如何禁用 link(仅禁用是因为我必须重新启用 link)我曾尝试使用此 document.getElementById("aadd").href = "Detail.aspx?using=add";
但是当我加载列表时元素动态我不能这样做。因为我问你如何禁用 link 所以我不能使用它并重新启用它?
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>
要禁用页面上的所有链接,您可以使用此代码:
document.body.addEventListener("click", function stopLinks(e){
if (this.nodeName==="A"){
e.preventDefault();
return false;
}
})
并重新启用它们:
document.body.removeEventListener("click", stopLinks);
如果您只想禁用特定元素内的链接,请将 document.body
替换为适当的元素。