用javascript或jquery更改链接的域部分 [英] change domain portion of links with javascript or jquery
本文介绍了用javascript或jquery更改链接的域部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
很抱歉我的原始问题不清楚,希望通过重新措辞我可以更好地解释我想要做什么。
Sorry for my original question being unclear, hopefully by rewording I can better explain what I want to do.
因为这个我需要一种方法来使用JavaScript(或jQuery)执行以下操作:
Because of this I need a way to use JavaScript (or jQuery) to do the following:
- 确定当前正在访问的页面的域
- 标识页面上使用域名www.domain1.com并替换为www.domain2.com的所有链接
即如果用户正在访问www.domain2.com/index,那么:
i.e. if the user is accessing www.domain2.com/index then:
<a href="www.domain1.com/contentpages/page.html">Test 1</a>
应在加载时动态重写
<a href="www.domain2.com/contentpages/page.html">Test 1</a>
甚至可以只重写 href中的部分网址
tag?
Is it even possible to rewrite only a portion of the url in an href
tag?
推荐答案
您的代码将遍历页面上的所有链接。这是一个只迭代需要替换的URL的版本。
Your code will loop over all links on the page. Here's a version that only iterates over URLS that need to be replaced.
var linkRewriter = function(a, b) {
$('a[href*="' + a + '"]').each(function() {
$(this).attr('href', $(this).attr('href').replace(a, b));
});
};
linkRewriter('originalDomain.com', 'rewrittenDomain.com');
这篇关于用javascript或jquery更改链接的域部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文