使用JS在新窗口中打开HREF

时间:2016-11-21 10:37:01

标签: javascript

我正在尝试使用JS向锚点/ div添加URL。我已经实现了以下代码,但它只会在同一个窗口中打开URL。

 var href = document.getElementById("mainwrapper");
 href.setAttribute('onclick', 'window.location.href=\'http://www.ebay.com/\'');'

使用此方法打开目标的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

你需要使用window.open()函数的第二个参数来做到这一点。

var href = document.getElementById("mainwrapper"); 
href.addEventListener("click", handleOpen);

function handleOpen(){
   window.open('http://www.ebay.com','_blank');
}
相关问题