我想更改我的博客上的所有外部链接(这里是 blogspot,这就是我寻找 jQuery 代码的原因)而不更改我的博客的发布,因为如果我这样做,我需要做很多工作。
例如,我的网站是 example.com
。
我想将所有外部链接更改为
http://example.com/p/go.html?url=http://externallink.com
无需对我的博客文章进行任何更改。我不知道从何开始。
已解决:https://gist.github.com/2342509 谢谢大家:D 我只需要稍微改变一下。
最佳答案
在 jQuery 中你可以尝试:
// DOM ready
$(function(){
$('a[target="_blank"]').prop('href', 'http://example.com/p/go.html?url=http://externallink.com');
});
当然,只有当您在 HTML 中设置了 target="_blank"
属性/属性并且希望所有链接都打开相同的 URL 时,这才有效。这个想法源于您希望在不同的选项卡/窗口中自动打开外部链接的事实。
如果这不是必需的功能,您可以以类似的方式使用自定义 data-
属性。唯一的区别是您需要循环每个链接并从中获取数据。
// DOM ready
$(function(){
$('a[data-href]').each(function(){
var anc = $(this),
href = anc.prop('href'),
dataHref = anc.data('href');
anc.prop('href', href + '?url=' + dataHref);
});
});
HTML 示例:
<a href="http://example.com/p/go.html" data-href="http://externallink.com">external link</a>
现在,如果这仍然不是您想要的,您可能需要添加更多信息。
关于javascript - 使用 jQuery 更改所有外部链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28912216/