jQuery:使用 target="_blank" 转到 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6673473/
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: go to URL with target="_blank"
提问by Dimitri Vorontzov
I am using this bit of jQuery code to get href of the link:
我正在使用这段 jQuery 代码来获取链接的 href:
var url = $(this).attr('href');
-- and this bit of code to go to that href:
-- 还有这段代码去那个href:
window.location = url;
Everything is just the way I want it, except the new page opens in the same window as the previous one, and I want it to open in a new window or tab (something that in plain html would have been achieved by using target="_blank" formula).
一切都是我想要的方式,除了新页面在与前一个相同的窗口中打开,并且我希望它在新窗口或选项卡中打开(在纯 html 中可以通过使用 target=" _blank”公式)。
Question: How can I open the href in the new window or tab with jQuery?
问题:如何使用 jQuery 在新窗口或选项卡中打开 href?
Thank you for your help!
感谢您的帮助!
回答by Christopher Armstrong
You need to open a new window:
您需要打开一个新窗口:
window.open(url);
回答by brenjt
Use,
用,
var url = $(this).attr('href');
window.open(url, '_blank');
Update:the href
is better off being retrieved with prop since it will return the full url and it's slightly faster.
更新:href
最好使用 prop 进行检索,因为它会返回完整的 url,而且速度会稍快一些。
var url = $(this).prop('href');
回答by defau1t
Question: How can I open the href in the new window or tab withjQuery?
问题:如何使用jQuery在新窗口或选项卡中打开 href?
var url = $(this).attr('href').attr('target','_blank');
回答by recurse
Detect if a target attribute was used and contains "_blank". For mobile devices that don't like "_blank", this is a reliable alternative.
检测是否使用了目标属性并包含“_blank”。对于不喜欢“_blank”的移动设备,这是一个可靠的选择。
$('.someSelector').bind('touchend click', function() {
var url = $('a', this).prop('href');
var target = $('a', this).prop('target');
if(url) {
// # open in new window if "_blank" used
if(target == '_blank') {
window.open(url, target);
} else {
window.location = url;
}
}
});
回答by Vidit Anjaria
The .ready
function is used to insert the attribute once the page has finished loading.
该.ready
函数用于在页面加载完成后插入属性。
$(document).ready(function() {
$("class name or id a.your class name").attr({"target" : "_blank"})
})
回答by aroth
If you want to create the popup window through jQuery then you'll need to use a plugin. This one seems like it will do what you want:
如果您想通过 jQuery 创建弹出窗口,那么您需要使用插件。这个似乎会做你想做的事:
http://rip747.github.com/popupwindow/
http://rip747.github.com/popupwindow/
Alternately, you can always use JavaScript's window.openfunction.
或者,您始终可以使用 JavaScript 的window.open函数。
Note that with either approach, the new window must be opened in response to user input/action (so for instance, a click on a link or button). Otherwise the browser's popup blocker will just block the popup.
请注意,无论采用哪种方法,新窗口都必须打开以响应用户输入/操作(例如,单击链接或按钮)。否则浏览器的弹出窗口拦截器只会阻止弹出窗口。
回答by L?h?rü
Try using the following code.
尝试使用以下代码。
$(document).ready(function(){
$("a[@href^='http']").attr('target','_blank');
});