Jquery 在新标签页中打开 (_blank)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14950269/
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 Open in new Tab (_blank)
提问by user2085752
I've setup some Jquery based off other StackOverflow questions/answers. The purpose of this script is that it makes an entire div a link based on any a href tag that is inside that div.
我已经根据其他 StackOverflow 问题/答案设置了一些 Jquery。这个脚本的目的是使整个 div 成为一个基于该 div 内的任何 href 标签的链接。
This works fine however I need to set it to _blank to open in a new tab. I've tried the below with no luck.
这工作正常,但是我需要将其设置为 _blank 以在新选项卡中打开。我试过下面没有运气。
$(document).ready(function() {
$(".product-item").click(function() {
$(this).target = "_blank";
window.location = $(this).find("a").attr("href");
return false;
});
});
EDIT
编辑
Thanks for the help but none of these answers actually work. If anyone can paste the full code that actually works, rather than little snippets without testing if it works. Thanks.
感谢您的帮助,但这些答案都没有实际工作。如果有人可以粘贴实际有效的完整代码,而不是没有测试它是否有效的小片段。谢谢。
EDIT 2
编辑 2
Thanks to kristinalim, for providing a complete working solution.
感谢kristinalim提供完整的工作解决方案。
回答by kristinalim
Setting links on the page woud require a combination of @Ravi and @ncksllvn's answers:
在页面上设置链接需要结合@Ravi 和@ncksllvn 的回答:
// Find link in $(".product-item") and set "target" attribute to "_blank".
$(this).find("a").attr("target", "_blank");
For opening the page in another window, see this question: jQuery click _blankAnd see this referencefor window.open
options for customization.
要在另一个窗口中打开页面,请参阅此问题:jQuery click _blank并参阅此参考以window.open
获取自定义选项。
Update:
更新:
You would need something along:
你需要一些东西:
$(document).ready(function() {
$(".product-item").click(function() {
var productLink = $(this).find("a");
productLink.attr("target", "_blank");
window.open(productLink.attr("href"));
return false;
});
});
Note the usage of .attr()
:
请注意以下用法.attr()
:
$element.attr("attribute_name") // Get value of attribute.
$element.attr("attribute_name", attribute_value) // Set value of attribute.
回答by ncksllvn
Replace this line:
替换这一行:
$(this).target = "_blank";
With:
和:
$( this ).attr( 'target', '_blank' );
That will set its HREF to _blank.
这会将其 HREF 设置为 _blank。
回答by Ravi Gadag
you cannot set target attribute to div, becacuse div does not know how to handle http requests. instead of you set target attribute for link tag.
您不能将目标属性设置为 div,因为 div 不知道如何处理 http 请求。而不是您为链接标签设置目标属性。
$(this).find("a").target = "_blank";
window.location= $(this).find("a").attr("href")
回答by techfoobar
window.location
alwaysrefers to the location of the current window. Changing it will affect onlythe current window.
window.location
总是指当前窗口的位置。更改会影响只是当前窗口。
One thing that can be done is forcing a click on the link after setting its target
attribute to _blank
:
可以做的一件事是在将其target
属性设置为后强制单击链接_blank
:
检查这个:http : //www.techfoobar.com/2012/jquery-programmatically-clicking-a-link-and-forcing-the-default-action
Disclaimer:Its my blog.
免责声明:这是我的博客。