【问题标题】:How change all the href with a specific url?如何使用特定的 url 更改所有 href?
【发布时间】:2020-02-14 10:36:55
【问题描述】:

我想更改主页中所有 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);

                  }

【问题讨论】:

    标签: javascript jquery html arrays href


    【解决方案1】:

    一个&lt;a&gt; 节点has many properties

    给定:

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

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

    • link.href https://www.example.com:80/this/is/the/path?foo=bar&amp;bish=bash#testSector
    • link.hostname www.example.com
    • 链接路径名 /this/is/the/path
    • link.hash myAnchor

    这些和其他属性是读/写的。

    所以从问题来看,您似乎想测试a.hostname + a.pathname。如果是这样,那么以下内容将满足您的要求:

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

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

    注意事项:

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

    【讨论】:

      【解决方案2】:

      因为$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>

      【讨论】:

      • 你好 Phong,我试图做的是浏览网站主页上的所有链接 (
      【解决方案3】:

      您将此标记为 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");
      });
      

      【讨论】:

        【解决方案4】:

        您的解决方案的问题是“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,不一定是相对 url。我不确定您的示例网址是否有效。需要一个方案或至少 // 在开始时。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-02-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-03
          • 1970-01-01
          相关资源
          最近更新 更多