我希望當用戶單擊鏈接時,默認情況下,網站中的所有鏈接都會打開到新的背景選項卡。我已經檢查了所有,但他們都沒有提供適用於所有瀏覽器的解決方案。我已經提到了以下兩個鏈接給我寫代碼:使用javascript/jquery打開新背景選項卡中的鏈接
Suggested modifications for ie
我有以下代碼:
<html>
<head>
<style type="text/css">
.link{ color: #0055ff; cursor: pointer; text-decoration: underline; }
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".link").on('click', function(event){
var $this = $(this);
var a = document.createElement("a");
a.href = $this.href;
if(document.createEvent) {
var evObj = document.createEvent('MouseEvents');
evObj.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,
true, false, false, true, 1, a);
a.dispatchEvent(evObj);
} else if(document.createEventObject) {
var evObj = document.createEventObject();
evObj.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,
true, false, false, true, 1, a);
a.fireEvent('onclick', evObj);
}
});
});
</script>
</head>
<body>
<span class="link" data-href="http://www.google.com">Go to Google</span>
</body>
</html>
可能重複[javascript window.location在新標籤](http://stackoverflow.com/questions/7554108/javascript-window-location-in-new-tab) – Andy
谷歌爲window.open –
在鏈接上以編程方式觸發鼠標事件將觸發瀏覽器的彈出窗口阻止程序。如果鏈接被點擊,只需做一個簡單的'window.open'。 – Christoph