【问题标题】:Change part of link with part of current URL用当前 URL 的一部分更改链接的一部分
【发布时间】:2018-12-28 18:30:57
【问题描述】:

我是这方面的新手,我很难尝试做一些我认为必须简单的事情。

我在https://www.something.com/path/NUMBER/path/path 上托管了很多页面

在这些页面中,我有一个链接到https://www.example.com/path/REPLACE/path的按钮

我想将链接上的 REPLACE 更改为地址栏上的 NUMBER。

这是我的代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>

<body>

  <script>
      $(document).ready(function(){
          var newURL = window.location.protocol + "://" + window.location.host + "/" + window.location.pathname;
          pathArray = window.location.pathname.split( '/' );
          var part_2 = pathArray[2];
          var mylink = "https://www.example.com/path/" + part_2 + "/path/page.html";
      });    
  </script>

<p><a href="#" id="mylink">BUTTON</a></p>

</body>
</html>

非常感谢您的帮助!

【问题讨论】:

  • $("#mylink").attr("href", mylink);怎么样
  • 你想在加载的时候改变url还是直接改变文件中的链接?
  • 我想在加载时这样做,因为页面可能会改变它的路径
  • 我已经在我的回答中分享了这一点stackoverflow.com/a/51442820/1262248

标签: javascript jquery html url href


【解决方案1】:

试试这个。 网址1:-https://www.something.com/path/NUMBER/path/path; Url2:-https://www.example.com/path/REPLACE/path;

url2 = url2.split('/').map(a => {
    if(a == 'REPLACE') 
        return 'NUMBER'; 
    else 
        return a;
}).join('/');

【讨论】:

    【解决方案2】:

    如果您想使用 Javascript 进行更改,您可以执行以下操作:

    var url = "https://www.something.com/path/NUMBER/path/path";
    var oldUrl = "https://www.example.com/path/REPLACE/path/path";
    var regex = /https:\/\/www\.something\.com\/path\/(\w*?)\/.*/g;
    var match = regex.exec(url);
    oldUrl = oldUrl.replace("/REPLACE/", "/"+match[1]+"/");
    console.log(oldUrl);

    -- 编辑--

    另一个使用浏览器 URL 替换并替换第二个路径而不考虑剩余 URL 或路径的示例。

    $(document).ready(function() {
      var url = window.location.href;
      // As the script executed in iframe, assigning with hardcode value
      url = "https://stackoverflow.com/questions/51442637/change-part-of-link-with-part-of-current-url#";
      var oldUrl = "https://www.example.com/path/REPLACE/path/path";
      var regex = /https:\/\/\w*?\.?\w*?\.\w*?\/\w*?\/(\w*?)\/.*/g;
      var match = regex.exec(url);
      oldUrl = oldUrl.replace("/REPLACE/", "/" + match[1] + "/");
      document.getElementById("mylink").href = oldUrl;
    });
    <!doctype html>
    <html>
    
    <head>
      <meta charset="utf-8">
      <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    </head>
    
    <body>
    
    
      <p><a href="https://stackoverflow.com/questions/51442637/change-part-of-link-with-part-of-current-url#" id="mylink">BUTTON</a></p>
    
    </body>
    
    </html>

    【讨论】:

    • 谢谢!但是如果我想从 window.location.href 获取 oldUrl 怎么办?并且只使用第二条路径而不介意网址的其余部分。
    • 分享了另一个例子。希望对你有帮助
    • 像这样??

      按钮

    • 可以这样,也可以点击运行代码sn -p查看输出
    • 您是否正在使用 javascript/jquery 更新按钮中的更新 URL?
    猜你喜欢
    • 2019-09-06
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    相关资源
    最近更新 更多