[英]jQuery making a whole Div a clickable link with _blank
我正在嘗試創建整個Div可點擊的鏈接,並鏈接到新標簽頁。 到目前為止,我已經使div可單擊,並使用此代碼發送到新鏈接
$(document).ready(function(){
$('.wish-list').click(function(){
location.href = 'https://link.com'
})
});
這可以將您所在的頁面發送到鏈接,但是我想執行相同的操作,但是打開一個新標簽頁。
非常感謝您的幫助。
您可以使用window.open :
window.open("http://link.com", "_blank");
您可以使用:
window.open('url', '_blank');
用它代替location.href
window.open(strUrl, strWindowName[, strWindowFeatures]);
例
window.open('http://www.google.com','_blank');
您需要使用window.open()
。 由於沙箱操作,下面的代碼段無法在Stack Overflow代碼段環境中使用,但您可以在此處看到它的工作方式:
$(function(){ $('.link').on("click", function(){ window.open(this.dataset.url); }); });
.link { cursor:pointer; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="link" data-URL="http://espn.com">ESPN</div>
嘗試這個
window.open('url', '_blank');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.