Jquery在新标签中打开(_blank)

参见英文答案 > How can I open a link in a new window?10个
我根据其他StackOverflow问题/答案设置了一些Jquery.这个脚本的目的是它使整个div成为基于该div内的任何href标记链接.

这工作正常,但我需要将其设置为_blank以在新选项卡中打开.我已经尝试过以下没有运气了.

$(document).ready(function() {
    $(".product-item").click(function() {
        $(this).target = "_blank";
        window.location = $(this).find("a").attr("href");
        return false;
    });
});

编辑

感谢您的帮助,但这些答案都不起作用.如果有人可以粘贴实际工作的完整代码,而不是没有测试的小片段,如果它工作.谢谢.

编辑2

感谢kristinalim,提供完整的工作解决方案.

解决方法

页面上设置链接需要@ravi和@ncksllvn的答案组合:
// Find link in $(".product-item") and set "target" attribute to "_blank".
$(this).find("a").attr("target","_blank");

要在另一个窗口中打开页面,请参阅此问题:jQuery click _blank有关自定义的window.open选项,请参阅this reference.

更新:

你需要一些东西:

$(document).ready(function() {
  $(".product-item").click(function() {
    var productLink = $(this).find("a");

    productLink.attr("target","_blank");
    window.open(productLink.attr("href"));

    return false;
  });
});

注意.attr()用法

$element.attr("attribute_name")                   // Get value of attribute.
$element.attr("attribute_name",attribute_value)  // Set value of attribute.

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: <span id=&quot...
jQuery 添加水印 <script src="../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...