jQuery更改页面中的所有链接

Bun*_*bit 3 jquery

 $(function(){
            $('a').each(function(){
                        var x=this.href;
                        this.href="www.somesitename.com/filter"+this.href;
                  });
         });
Run Code Online (Sandbox Code Playgroud)

我写了上面的jQuery脚本,将一些网站名称附加到页面中的所有链接,但它没有按预期工作.

Dar*_*rov 29

您可能需要添加http://:

$(function(){
    $('a').each(function() {
        $(this).attr('href', 'http://www.somesitename.com/filter' + this.href);
    });
});
Run Code Online (Sandbox Code Playgroud)


bal*_*dre 13

我想知道为什么你会使用jQuery这样的基本行为...

这就是为什么标签BASE是有

为页面上的所有链接指定默认URL默认目标:

<head>
    <base href="http://www.w3schools.com/images/" />
    <base target="_blank" />
</head>

<body>
    <img src="stickman.gif" />
    <a href="http://www.w3schools.com">W3Schools</a>
</body>
Run Code Online (Sandbox Code Playgroud)

不要简单的复杂化!


添加

来自Sitepoint

如果您的所有相关链接表单提交都转到同一位置,那么基本元素将对您有用.