jQuery在新标签页中打开(_blank) [英] Jquery Open in new Tab (_blank)

查看:111
本文介绍了jQuery在新标签页中打开(_blank)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经基于其他StackOverflow问题/答案设置了一些Jquery.该脚本的目的是使整个div成为基于该div内任何href标记的链接.

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.

这很好,但是我需要将其设置为_blank才能在新标签页中打开.我已经尝试过以下方法,但是没有运气.

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.

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

编辑

感谢您的帮助,但这些答案实际上都无效.如果有人可以粘贴实际有效的完整代码,而不是粘贴一些简短的代码而不进行测试是否有效.谢谢.

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.

编辑2

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

Thanks to kristinalim, for providing a complete working solution.

推荐答案

在页面上设置链接需要使用@Ravi和@ncksllvn的答案:

Setting links on the page woud require a combination of @Ravi and @ncksllvn's answers:

// Find link in $(".product-item") and set "target" attribute to "_blank".
$(this).find("a").attr("target", "_blank");

要在另一个窗口中打开页面,请参见以下问题: jQuery单击_blank 并参见

For opening the page in another window, see this question: jQuery click _blank And see this reference for window.open options for customization.

更新:

您将需要一些东西:

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

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

    return false;
  });
});

请注意 .attr() 的用法:

Note the usage of .attr():

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

这篇关于jQuery在新标签页中打开(_blank)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆