web-dev-qa-db-ja.com

href属性を削除する

ページネーションのコードを書こうとしています。 1つの機能は、現在のリンクを無効にして、テキストのように見せ、クリックできないようにすることです。 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>を達成できますか?

8
danny alkhald

これを試してください removeAttribute( "href")

21
vladkras

添付のコードスニペットをお試しください。 javascriptを使用し、現在3番目のリンクへのリンク関数を削除しています。リンクを削除したい行を反映するように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>
1
Toolbox