如何使用jQuery更改当前URL并在单击时打开新选项卡

How to change current URL and open new tab on click using jQuery

本文关键字:新选项 选项 单击 jQuery 何使用 URL      更新时间:2023-09-26

我需要这个来替换页面上具有当前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 是个更好的主意?

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

/* 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"); */
     });
});

函数单击(重新源){

            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>

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

<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>