如何用特定的 url 更改所有 href?

How change all the href with a specific url?

我想更改我主页中 href = "" 的所有 url,但它是特定的 url。

示例:

<a href="mydomain.com/issues/resolve"> The issue </a>

切换到:

<a href = "mydomain.com/issues/resolve/#thecontent">The issue</a>

我澄清一下,相同的不止一个url,我要全部改掉

我已经试过了,没有任何可见的结果,我正在用 javascript:

$enlaces = document.getElementsByTagName("a");

                  for (let i = 0; i < $enlaces.length; i++) {
                    $enlaces2 = $enlaces[i];

                    if ($enlaces[i].href == 'mydomain.com/issues/resolve') {
                        this.href = 'mydomain.com/issues/resolve/#thecontent';
                    }

                    console.log ($enlaces2);

                  }

因为$enlaces[i].href给了你your_domain+href的值,所以if子句中的条件不会发生。

您需要使用 attr('href') 获得准确的 href 值,如下所示

$enlaces = document.getElementsByTagName("a");

for (let i = 0; i < $enlaces.length; i++) {
  $enlaces2 = $enlaces[i];
  var href_DOM_value = $($enlaces[i]).attr('href');
  var href_Actual_value = $enlaces[i].href;
  console.log(href_DOM_value);
  console.log(href_Actual_value);
  
  if (href_DOM_value == 'mydomain.com/issues/resolve') {
      $($enlaces2).attr('href', 'mydomain.com/issues/resolve/#thecontent');
      console.log("changed");
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="mydomain.com/issues/resolve"> The issue </a>

你将此标记为 jQuery 问题,我会给你一个 jQuery 答案:

$("a").each(function(){
if($(this).atrr("href")=="mydomain.com/issues/resolve/")
 $(this).atrr("href","mydomain.com/issues/resolve/#thecontent");
});

也就是说,使用 == "mydomain.com/issues/resolve/" 是一种不好的做法,因为通常 href 属性包含协议,所以我会改用 indexOf

$("a").each(function(){
if($(this).atrr("href").indexOf("mydomain.com/issues/resolve/") >= 0)
 $(this).atrr("href","mydomain.com/issues/resolve/#thecontent");
});

您的解决方案的问题是 "this" 未定义。我可能在链上更高,但它不是由 for 循环设置的。你想要的更像

$enlaces = document.getElementsByTagName("a");

                  for (let i = 0; i < $enlaces.length; i++) {
                    $enlaces2 = $enlaces[i];

                    if ($enlaces[i].href == 'mydomain.com/issues/resolve') {
                        $enlaces[i].href = 'mydomain.com/issues/resolve/#thecontent';
                    }

                    console.log ($enlaces2);

                  }

其他一些答案可能会解决其他问题,例如 href 给出完整的 url with scheme,不一定是相对的 url。我不确定您的示例 url 是否有效。需要一个方案或至少 // 在开始时。

一个<a>节点has many properties

鉴于:

  • HTML: <a href="https://www.example.com:80/this/is/the/path?foo=bar&bish=bash#myAnchor" >test link</a>,
  • javascript 成员表示生成的 DOM 节点: link,

那么这里感兴趣的属性是:

  • link.href https://www.example.com:80/this/is/the/path?foo=bar&bish=bash#testSector
  • link.hostname www.example.com
  • link.pathname /this/is/the/path
  • link.hash myAnchor

这些和其他属性是 read/write。

所以从问题来看,你似乎想测试a.hostname + a.pathname。如果是这样,那么以下将执行您想要的操作:

$('a').each(function() {
    if ((this.hostname + this.pathname) == 'mydomain.com/issues/resolve') {
        this.hash = 'thecontent';
    }
});

您应该明白为什么测试 .href 属性 不起作用。

备注:

  • 读取:a.hash returns 一个包含前导的字符串 #
  • 写入:a.hash = "someString"不需要散列。
  • 在这方面浏览器之间存在历史差异,但我认为它们现在已经解决了(值得测试)。