2016-11-21 83 views
0

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

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

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

回答

0

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

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

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