Iev*_*nov 233
这将打开一个新窗口,而不是tab(使用JavaScript,但非常简洁):
<a href="print.html"
onclick="window.open('print.html',
'newwindow',
'width=300,height=250');
return false;"
>Print</a>
Run Code Online (Sandbox Code Playgroud)
Chr*_*oph 129
使用纯HTML,你无法影响这一点 - 每个现代浏览器(=用户)都可以完全控制这种行为,因为它在过去被滥用了很多...
anchor元素具有属性target
*.您要寻找的价值是_blank
**.
<a href="www.example.com/example.html" target="_blank">link text</a>
Run Code Online (Sandbox Code Playgroud)
它会打开一个新窗口(HTML4)或一个新的浏览上下文(HTML5).但是,在现代浏览器中浏览上下文主要是"新选项卡"而不是"新窗口".你没有影响,你不能"强迫"浏览器打开一个新窗口.
另一方面,可以通过javascript强制一个新的窗口 - 请参阅下面的Ievgen答案的javascript解决方案.但是,请注意,通过javascript打开窗口(如果没有在锚点元素的onclick事件中完成)可能会受到弹出窗口阻止程序的阻止!
(*)此属性可追溯到浏览器没有选项卡的时间,并且使用框架集是最先进的.与此同时,此属性的功能略有变化(请参阅MDN Docu)
(**)还有一些其他值没有多大意义(因为它们的设计考虑了框架集)_parent
,_self
或者_top
.
Cuz*_*uzi 29
我知道它有点老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));
});
Run Code Online (Sandbox Code Playgroud)
它将<a target="_new">
在一个新窗口中打开所有内容
编辑:
1,我在原始代码中做了一些小改动现在它完全按照用户屏幕比例打开新窗口(对于横向桌面)
但是,我建议您使用以下代码,如果您在移动设备中打开新标签中的链接(感谢其他问题中的zvona回答):
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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
400811 次 |
最近记录: |