我无法在新窗口(而不是标签页)中打开图像。
此脚本位于<head>
:
<script type="text/javascript">
function randomImg1() {
var myImage1 = new Array();
myImage1[0] = "1.jpg";
myImage1[1] = "2.jpg";
myImage1[2] = "3.jpg";
var random = Math.floor(Math.random() * myImage1.length);
document.getElementById("image").innerHTML = "<img src='"
+ myImage1[random] + "' alt='image'></img>";
}
</script>
我的按钮位于<body>
:
<button onclick="randomImg1();OpenInNewTab();">click</button>
<div id="image"></div>
答案 0 :(得分:0)
使用https://www.w3schools.com/jsref/met_win_open.asp打开一个新窗口。添加spec
参数以使其作为新窗口而不是选项卡打开。并确保通过图像的完整路径:
<script type="text/javascript">
function randomImg1() {
var myImage1 = new Array();
myImage1[0] = "1.jpg";
myImage1[1] = "2.jpg";
myImage1[2] = "3.jpg";
var random = Math.floor(Math.random() * myImage1.length);
// create the full image URL
var imageUrl = window.location.protocol + '//' + window.location.host + window.location.pathname + myImage1[random];
// open in a new window. ideally use the image's size for w and h
window.open(imageUrl, '_blank', 'height=300,width=300');
}
</script>