删除href属性

14

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

我正試圖編寫一個分頁程式碼。其中一個功能是禁用當前鏈接,使其看起來像文本並且無法被點擊。在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>链接</a>


你看过 https://dev59.com/CEzSa4cB1Zd3GeqPqNkI 吗? - J Max
1
你尝试过使用 removeAttribute 吗?https://developer.mozilla.org/zh-CN/docs/Web/API/element.removeAttribute - zdyn
1
使用 removeAttribute 从元素中移除属性。 - Musa
@J Max 是的,但那并没有帮助。正如我所描述的,我不是想阻止链接的默认操作,也不是让它“display: 'none'”,我正在寻找其他东西。 - danny alkhald
@zdyn,@Musa,谢谢。removeAttribute正好符合我的需求。 - danny alkhald
2个回答

36

1
谢谢。removeAttribute 正好符合我的需求。 - danny alkhald
可能考虑更新链接到 MDN -- https://developer.mozilla.org/zh-CN/docs/Web/API/Element/removeAttribute - sfletche

3

请尝试以下代码片段。 它使用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>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接