使用 jQuery 在新标签页中打开所有链接 [英] Open all links in new tabs with jQuery
本文介绍了使用 jQuery 在新标签页中打开所有链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些链接通过 JSON 动态放置在我的页面中,无法直接编辑它们.我想强制所有链接在新标签页中打开,ala target="_blank"
I have some links that are placed in my page dynamically via JSON and have no way to directly edit them. I want to force all links to open in new tabs, ala target="_blank"
认为这会起作用..但遗憾的是它不是.有什么想法吗?
Thought this would work.. but sadly it isn't. Any ideas?
$('a').attr("target","_blank");
这是一个带有动态代码的jsFiddle:http://jsfiddle.net/danielredwood/mrgta/7/
Here's a jsFiddle with the dynamic code: http://jsfiddle.net/danielredwood/mrgta/7/
推荐答案
您可以这样做(让用户浏览器决定是否打开新窗口或选项卡)
You could do this (which lets the users browser decide whether to open a new window or tab)
$('a').live('click', function() {
window.open($(this).attr('href'));
return false;
});
这篇关于使用 jQuery 在新标签页中打开所有链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文