2016-11-21 86 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'); 
}