Javascript 禁用指向当前页码的链接。(锚标签)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2528504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable link to current page number. (anchor tag)
提问by user285146
I have one anchor tag. I want to disable it after once it clicked. I tried the following way.
我有一个锚标签。单击后我想禁用它。我尝试了以下方法。
print "<script type='text/javascript'>parent.window.frames[2].document.getElementById('pagination').innerHTML+=\"<a href=# onclick='pageshow`($i);this.disabled=true'>$i</a> \"</script>";
Note: I don't want to disable the link. I want to disable the link operation. Like Google search page.
注意:我不想禁用链接。我想禁用链接操作。喜欢谷歌搜索页面。
Eg: 1 2 3 4 5 Next
例如:1 2 3 4 5 下一个
Here once I clicked the 1st page I can't click the same page again. Same thing I want.
在这里,一旦我点击了第一页,我就无法再次点击同一页面。我想要的一样。
回答by vivek
You can also disable the anchor tag by using the following way.
您还可以使用以下方式禁用锚标记。
<a href='URL' style='text-decoration:none;cursor: auto;' onClick='return false'>LINK</a>
回答by Quentin
Having reread the question…
重读了这个问题……
Google do not "Disable the link after it has been clicked".
谷歌不会“点击后禁用链接”。
They just don't link to the current page. Their markup is appalling so I won't copy it, a simplified example (for page 2) would be:
他们只是不链接到当前页面。他们的标记令人震惊,所以我不会复制它,一个简化的例子(第 2 页)是:
<ol>
<li><a>1</a></li>
<li>2</li>
<li><a>3</a></li>
<li><a>4</a></li>
</ol>
回答by Mike Lyons
The best answer I've seen to this question involves using jQuery.
我见过这个问题的最佳答案是使用 jQuery。
How to enable or disable an anchor using jQuery?
here's the code incase that link breaks.
This disables <a id="current">by preventing e(the click event on the anchor)
这是链接断开的代码。这<a id="current">通过阻止e(锚点上的点击事件)来禁用
$(document).ready(function() {
$('a#current').click(function(e) {
e.preventDefault();
});
});
回答by Quentin
<a href="…" onclick="this.removeAttribute('href');">
回答by eyelidlessness
Javascript isn't the right way to do this, but I'll provide an answer for the heck of it:
Javascript 不是执行此操作的正确方法,但我将为它提供一个答案:
var links = document.getElementsByTagName('a'), link, children;
for(var i = 0; i < links.length; i++) {
link = links[i];
if(link.href == window.location.href) {
children = link.childNodes;
for(var j = 0; j < children.length; j++) {
link.parentNode.insertBefore(children[j], link);
}
link.parentNode.removeChild(link);
}
}
Edit: I don't know if "he-double-hockey-sticks" is "foul language" on SO but if it is, this will be deleted mysteriously some months from now so I've used "heck" instead.
编辑:我不知道“he-double-hockey-sticks”是否是 SO 上的“粗言秽语”,但如果是,它将在几个月后神秘地被删除,所以我改用了“哎呀”。

