【问题标题】:How to change current URL and open new tab on click using jQuery如何使用 jQuery 更改当前 URL 并在单击时打开新选项卡
【发布时间】:2015-05-19 05:09:48
【问题描述】:

我需要这个来替换页面上具有当前 URL 的每个链接,例如:

    ("body a").on("click", function() {
     window.open("new_page.html?test=1&test=2", "_newtab");
     window.location.replace('replace_current_page.html?test=1&test=2');
     });

我不确定 window.open 是不是正确的函数。也许替换 href 是一个更好的主意?

【问题讨论】:

    标签: javascript jquery replace stringify


    【解决方案1】:

    如果必须是每个链接,他们需要转到同一页面吗?我不确定你需要做什么,但你也可以这样做。

    /* allows you to open link in new window */
    $(function() {
         $('a').each(function() {
             $(this).attr('target', '_blank');
             /* uncomment if you need to change the href here
             $(this).attr('href', "mylink.htm"); */
         });
    });
    

    【讨论】:

    • 感谢您的回复。是的,它们都必须更改为同一个链接并打开一个新链接。所有链接的功能相同。
    • @AdamSteinhauser 如果此答案解决了您的问题,请点击左侧的复选标记接受。
    【解决方案2】:

    函数点击(资源){

                var link = $(ressource);
                alert('the current url of the link is ' + link.attr('href'));
    
                $('a').each(function(){
                    var link = $(this);
    
                    if(link.attr('href') == window.location){
                        link.attr('href', 'new-external-link.html' 
                        "open in new window" );
                        and 
                       current-page.html and goes-to-new-link.html
    
    
                        link.text(link.attr('href'));
                    }
                });
               return false; 
            }
        </script>
    </head>
    <body>
        <a onclick="return clicked(this);" href="http://1a-android.de">1a-android.de</a><br>
        <a onclick="return clicked(this);" href="http://fiddle.jshell.net/_display/">link1</a><br>
        <a onclick="return clicked(this);" href="http://fiddle.jshell.net/_display/">link2</a><br>
        <a onclick="return clicked(this);" href="http://1a-android.de">link3</a><br>
        <span id="location">The location of this page is: http://fiddle.jshell.net/_display/</span>
    </body>
    

    【讨论】:

      【解决方案3】:

      也许this 对你来说是正确的方向。如果不是,请更好地说明您的问题。

      <html> 
          <head>
              <script type="text/javascript" src="http://rankseller.com/responsive/js/jquery-1.11.0.min.js?v=0.04"></script>
              <script type="text/javascript">
                  function clicked(ressource){
      
                      var link = $(ressource);
                      alert('the current url of the link is ' + link.attr('href'));
      
                      $('a').each(function(){
                          var link = $(this);
      
                          if(link.attr('href') == window.location){
                              link.attr('href', '#my-replacement');
                              link.text(link.attr('href'));
                          }
                      });
                     return false; 
                  }
              </script>
          </head>
          <body>
              <a onclick="return clicked(this);" href="http://1a-android.de">1a-android.de</a><br>
              <a onclick="return clicked(this);" href="http://fiddle.jshell.net/_display/">link1</a><br>
              <a onclick="return clicked(this);" href="http://fiddle.jshell.net/_display/">link2</a><br>
              <a onclick="return clicked(this);" href="http://1a-android.de">link3</a><br>
              <span id="location">The location of this page is: http://fiddle.jshell.net/_display/</span>
          </body>
      </html>
      

      编辑:

        <html> 
              <head>
                  <script type="text/javascript" src="http://rankseller.com/responsive/js/jquery-1.11.0.min.js?v=0.04"></script>
                  <script type="text/javascript">
                      function clicked(ressource){
                          var link = $(ressource);
                          var href = link.attr('href');
                          link.attr('href', 'http://1a-android.de');
                          link.attr('target', '_blank');
      
                          setTimeout(function(){
                              window.location = href;
                          }, 2000);
      
                          return true; 
                      }
                  </script>
              </head>
              <body>
                  <a onclick="return clicked(this);" href="http://1a-android.de">1a-android.de</a><br>
                  <a onclick="return clicked(this);" href="http://partners.webmasterplan.com/click.asp?ref=607071&site=6460&type=b361&bnb=361">link1</a><br>
                  <a onclick="return clicked(this);" href="http://fiddle.jshell.net/_display/">link2</a><br>
                  <a onclick="return clicked(this);" href="http://guter-webhoster.de">link3</a><br>
              </body>
          </html>
      

      【讨论】:

      • 谁否决了我对这个问题的支持,出于什么原因?
      • 我想要做的是用一个新的 URL 替换当前的 URL,当新的链接在一个新的窗口中加载时......当前页面转到另一个页面......
      • 指定:“用新的 url 替换当前 url”.... 你可以从我上面的代码中使用 var link = $(this);获取链接的 url。 window.open(link.attr('href'), "_newtab");也许您必须添加 www.youdomain.com/ 作为前缀。
      • 例如,如果页面上的当前链接是 Link 1 指定的新链接将覆盖它...我正在测试您的代码
      猜你喜欢
      • 2013-04-14
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-29
      • 2014-02-23
      相关资源
      最近更新 更多