我正在尝试创建一个随机的链接选择器--当我按下按钮时,链接就会出现在当前标签页中。我正在试着让它变成一个新的。
<script type="text/javascript">
function randomlinks(){
var myrandom=Math.round(Math.random()*2)
var links=new Array()
links[0]="link1.com"
links[1]="link2.com"
links[2]="link3.com"
window.location=links[myrandom]
}
</script>
<form>
<input type="button" value="Random Episode!" onClick="randomlinks()">
</form>
发布于 2018-07-11 03:01:30
将其更改为:
//This sets the location of the current window, which you don't want
window.location = links[myrandom];
要这样做:
//This says to open a new tab or window, based on the user browser settings, to the url
window.open(links[myrandom]);
发布于 2018-07-11 03:01:10
只需替换这行
window.location=links[myrandom]
使用
window.open(links[myRandom], '_blank');
发布于 2018-07-11 03:50:49
target属性指定打开链接文档的位置。您还必须先检查您的浏览器设置。如果它们被设置为新窗口,您可以在整个生命周期中单击,它仍将在新窗口中打开,而不是在新选项卡中打开。
这些都是正确的属性。
目标属性可以具有下列值之一:
_blank - Opens the linked document in a new window or tab
_self - Opens the linked document in the same window/tab as it was clicked (this is default)
_parent - Opens the linked document in the parent frame
_top - Opens the linked document in the full body of the window
framename - Opens the linked document in a named frame
因此,这意味着缺省值(没有target属性)
window.location=links[myrandom]
也在起作用。希望它能帮助你的脚本很好地工作。
https://stackoverflow.com/questions/51272324
复制相似问题