jQuery 更改页面中的所有链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2543196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery to change all the links in the page
提问by Bunny Rabbit
$(function(){
$('a').each(function(){
var x=this.href;
this.href="www.somesitename.com/filter"+this.href;
});
});
i wrote the above jQuery script to append some site name to all the links in the page but it's not working as expected.
我编写了上面的 jQuery 脚本来将一些站点名称附加到页面中的所有链接,但它没有按预期工作。
回答by Darin Dimitrov
You probably need to add http://
:
您可能需要添加http://
:
$(function(){
$('a').each(function() {
$(this).attr('href', 'http://www.somesitename.com/filter' + this.href);
});
});
回答by balexandre
I wonder why would you use jQuery for such basic behaviour...
我想知道你为什么要使用 jQuery 来处理这种基本的行为......
that is why the tag BASEis there for
这就是标签 BASE存在的原因
Specify a default URLand a default targetfor all linkson a page:
为页面上的所有链接指定默认 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>
don't complicate what is simple!
不要把简单的事情复杂化!
added
添加
from Sitepoint
从站点点
The base element is only going to be useful to you if all your relative linksor form submissionsgo to the same location.
只有当您所有的相关链接或表单提交都位于同一位置时,基本元素才会对您有用。
回答by esteewhy
Further improvement of Darin's solution:
进一步改进 Darin 的解决方案:
$(function(){
$('a').attr('href', (n, old) => 'http://www.somesitename.com/filter' + old);
});
One thing to notice that in case of .eachthis.href produces an absolute fully qualified URL (e.g.: http://localhost:8888/mylink) so result might look like: www.somesitename.com/filter/http://localhost:8888/mylink. The solution was to get attribute value via jQuery: $(this).attr('href'), which correctly yields /mylink.
需要注意的一件事是,在.eachthis.href 的情况下会生成一个绝对完全限定的 URL(例如:http://localhost:8888/mylink),因此结果可能如下所示:www.somesitename.com/filter/ http://本地主机:8888/mylink。解决方案是通过 jQuery 获取属性值:$(this).attr('href'),它正确地产生/mylink。
回答by pmko
define a base tag in your head and then update it's href property when needed.
在您的头脑中定义一个基本标签,然后在需要时更新它的 href 属性。
<base href="http://www.some-domain.com/" />
$("base").attr("href","http://www.some-other-domain.com/");
回答by CyberAbhay
Here is what i have used to do this
这是我用来做这个的
I have removed each 'blog/' from href on the page in the same you can change link or update it
我已经从页面上的 href 中删除了每个“博客/”,您可以更改链接或更新它
$('a').each(function(){
var updated_link = $(this).attr('href').replace('blog/', '');
$(this).attr('href', updated_link);
});
$('a').each(function(){
var updated_link = $(this).attr('href').replace('blog/', '');
$(this).attr('href', updated_link);
});
I have used replace function to update part of url.in your case you can update it
我已经使用替换功能来更新部分 url.in 你的情况你可以更新它
回答by Ankur
You will probably need to use the append() function jQuery has http://api.jquery.com/append/
您可能需要使用 jQuery 的 append() 函数http://api.jquery.com/append/