正在删除href属性

Removing the href attribute

本文关键字:属性 href 删除      更新时间:2023-09-26

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

<a>Link</a>

我在javaScript、中无法做到这一点

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

因为

this.current.href = null;

产生

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

我还尝试了this.current.href=""this.current.disabled=true,但它们都不起作用。如何实现<a>Link</a>

尝试此removeAttribute("href")

尝试附加的代码片段。它使用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>