jQuery:转到目标为“_blank”的 URL
我正在使用这段 jQuery 代码来获取链接的 href:
var url = $(this).attr('href');
-- 并使用这段代码转到该 href:
window.location = url;
一切都按照我想要的方式进行,除了新页面在与前一个页面相同的窗口中打开,并且我希望它在新窗口或选项卡中打开(在纯 html 中可以通过使用 target="_blank" 公式来实现)。
问题:如何使用 jQuery 在新窗口或选项卡中打开 href?
感谢您的帮助!
I am using this bit of jQuery code to get href of the link:
var url = $(this).attr('href');
-- and this bit of code to go to that 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).
Question: How can I open the href in the new window or tab with jQuery?
Thank you for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
检测目标属性是否已使用且包含“_blank”。对于不喜欢“_blank”的移动设备,这是一个可靠的替代方案。
Detect if a target attribute was used and contains "_blank". For mobile devices that don't like "_blank", this is a reliable alternative.
如果您想通过 jQuery 创建弹出窗口,那么您需要使用插件。这似乎会做你想做的事:
http://rip747.github.com/popupwindow/
或者,您始终可以使用 JavaScript 的 window.open 函数。
请注意,无论采用哪种方法,都必须打开新窗口以响应用户输入/操作(例如,单击链接或按钮)。否则浏览器的弹出窗口阻止程序将仅阻止弹出窗口。
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:
http://rip747.github.com/popupwindow/
Alternately, you can always use JavaScript's window.open function.
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.
尝试使用以下代码。
Try using the following code.
您需要打开一个新窗口:
https://developer.mozilla.org /en-US/docs/DOM/window.open
You need to open a new window:
https://developer.mozilla.org/en-US/docs/DOM/window.open
使用,
更新:使用 prop 检索
href
效果更好,因为它会返回完整的 url,而且速度稍快一些。Use,
Update:the
href
is better off being retrieved with prop since it will return the full url and it's slightly faster..ready
函数用于在页面加载完成后插入属性。The
.ready
function is used to insert the attribute once the page has finished loading.