首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Javascript/Html..按钮进入当前选项卡而不是新选项卡

Javascript/Html..按钮进入当前选项卡而不是新选项卡
EN

Stack Overflow用户
提问于 2018-07-11 02:57:10
回答 3查看 49关注 0票数 0

我正在尝试创建一个随机的链接选择器--当我按下按钮时,链接就会出现在当前标签页中。我正在试着让它变成一个新的。

代码语言:javascript
复制
<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>

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-07-11 03:01:30

将其更改为:

代码语言:javascript
复制
//This sets the location of the current window, which you don't want
window.location = links[myrandom];

要这样做:

代码语言:javascript
复制
//This says to open a new tab or window, based on the user browser settings, to the url
window.open(links[myrandom]);
票数 2
EN

Stack Overflow用户

发布于 2018-07-11 03:01:10

只需替换这行

代码语言:javascript
复制
window.location=links[myrandom]

使用

代码语言:javascript
复制
window.open(links[myRandom], '_blank');
票数 1
EN

Stack Overflow用户

发布于 2018-07-11 03:50:49

target属性指定打开链接文档的位置。您还必须先检查您的浏览器设置。如果它们被设置为新窗口,您可以在整个生命周期中单击,它仍将在新窗口中打开,而不是在新选项卡中打开。

这些都是正确的属性。

目标属性可以具有下列值之一:

代码语言:javascript
复制
_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属性)

代码语言:javascript
复制
window.location=links[myrandom]

也在起作用。希望它能帮助你的脚本很好地工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51272324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档