建立一個鏈接打開一個新窗口(而不是標簽)有沒有辦法讓鏈接打開一個新的瀏覽器窗口(而不是選項卡)而不使用JavaScript?
3 回答

慕的地8271018
TA貢獻1796條經(jīng)驗 獲得超4個贊
這將打開一個新窗口,而不是tab(使用JavaScript,但非常簡潔):
<a href="print.html" onclick="window.open('print.html', 'newwindow', 'width=300,height=250'); return false;" >Print</a>

12345678_0001
TA貢獻1802條經(jīng)驗 獲得超5個贊
我知道它有點舊Q但是如果你通過搜索解決方案來到這里,所以我通過jquery得到了一個很好的
jQuery('a[target^="_new"]').click(function() { var width = window.innerWidth * 0.66 ; // define the height in var height = width * window.innerHeight / window.innerWidth ; // Ratio the hight to the width as the user screen ratio window.open(this.href , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2));});
它將<a target="_new">
在一個新窗口中打開所有內容
編輯:
1,我在原始代碼中做了一些小改動現(xiàn)在它完全按照用戶屏幕比例打開新窗口(對于橫向桌面)
但是,我建議您使用以下代碼,如果您在移動設備中打開新標簽中的鏈接:
jQuery('a[target^="_new"]').click(function() { return openWindow(this.href);}function openWindow(url) { if (window.innerWidth <= 640) { // if width is smaller then 640px, create a temporary a elm that will open the link in new tab var a = document.createElement('a'); a.setAttribute("href", url); a.setAttribute("target", "_blank"); var dispatch = document.createEvent("HTMLEvents"); dispatch.initEvent("click", true, true); a.dispatchEvent(dispatch); } else { var width = window.innerWidth * 0.66 ; // define the height in var height = width * window.innerHeight / window.innerWidth ; // Ratio the hight to the width as the user screen ratio window.open(url , 'newwindow', 'width=' + width + ', height=' + height + ', top=' + ((window.innerHeight - height) / 2) + ', left=' + ((window.innerWidth - width) / 2)); } return false;}
添加回答
舉報
0/150
提交
取消