javascript 删除 href 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17387357/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:08:06  来源:igfitidea点击:

Removing the href attribute

javascripthyperlinkhref

提问by danny alkhald

I'm trying to write a code for pagination. One function is to disable the current link so it looks like text and to be unclickable. In html page this could be achieved by omitting the href attribute like :

我正在尝试编写分页代码。一种功能是禁用当前链接,使其看起来像文本并且不可点击。在 html 页面中,这可以通过省略 href 属性来实现,例如:

<a>Link</a>

I couldn't do that in javaScript,

我不能在 javaScript 中做到这一点,

AvdonPagination.prototype.manageLinks = function(link){
    if(link){
        this.current.href = '#';
        this.current = link;
    }else{
        this.current = this.links[0];
    }
    this.current.href = null;
}

because

因为

this.current.href = null;

produces

产生

<a href="null">Link</a>

Also I tried this.current.href="", and this.current.disabled=true, but neither of them works. How I can achieve <a>Link</a>?

我也试过this.current.href="", 和this.current.disabled=true,但它们都不起作用。我怎样才能实现<a>Link</a>

回答by vladkras

回答by Toolbox

Try the attached code snippet. It uses javascript and currently removes the link function to the third link. You can easily adapt it by adding more lines in javascript to reflect the lines you want to remove the link (but keep text).

尝试附加的代码片段。它使用 javascript,目前删除了指向第三个链接的链接功能。您可以通过在 javascript 中添加更多行来轻松调整它以反映您要删除链接的行(但保留文本)。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>

<a id="test-1" href="test-1">test-1</a>
<a id="test-2" href="test-2">test-2</a>
<a id="test-3" href="test-3">test-3</a>

<script>
document.getElementById("test-1").removeAttribute("href");
</script>

</body>
</html>