js生成的链接从新窗口打开的方法

 项目里用到了一个用js生成的链接

$("<a href='www.google.com.hk'>google</a>").appendTo(".example");

后来这个链接又要实现从新窗口打开,于是修改了一下代码。

方法一:

$("<a href='www.google.com.hk' target='_blank'>google</a>").appendTo(".example");

方法二:

给这个链接一个类名,再给这个类名一个点击自动加target属性的动作

$("<a href='www.google.com.hk' class='open_in_tab'>google</a>").appendTo(".example");

$('.zyzx_open_in_new_tab').click(function() {
if (!$(this).attr("target")) {
$(this).attr('target','_blank');
}
});

原文地址:https://www.cnblogs.com/youthdream/p/3440991.html