如何在新选项卡中的随机链接中打开 HTML 按钮 [英] How do I make a HTML button open in random links in new tab
本文介绍了如何在新选项卡中的随机链接中打开 HTML 按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我正在创建一个无用的网站,我需要制作一个按钮,将人们引导到新标签中的随机网站.我已经有了随机链接按钮代码,但我似乎无法使用新标签代码添加它.
So I am creating a uselesswebsite and I need to make a button that directs people to random websites in new tabs. I already have the random link button code but I cant seem to add it with the new tab code.
样品
var randomlinks=new Array(10)
randomlinks[0]="http://ducksarethebest.com/"
randomlinks[1]="http://cant-not-tweet-this.com/"
randomlinks[2]="http://just-shower-thoughts.tumblr.com/"
randomlinks[3]="http://www.fallingfalling.com/"
randomlinks[4]="http://www.partridgegetslucky.com/"
randomlinks[5]="http://ducksarethebest.com/"
randomlinks[6]="http://cant-not-tweet-this.com/"
randomlinks[7]="http://just-shower-thoughts.tumblr.com/"
randomlinks[8]="http://www.fallingfalling.com/"
randomlinks[9]="http://www.staggeringbeauty.com/"
randomlinks[10]="http://www.trypap.com/"
function randomlink(){
window.location=randomlinks[Math.floor(Math.random()*randomlinks.length)]
}
HTML
<form method="post">
<p><input type="button" name="B1" value="Random Link >>" onclick="randomlink()""window.open'"></p>
</form>
or
<a href="javascript:randomlink()">Random Link</a>
推荐答案
您需要使用 window.open(url)
而不是 window.location(url)
.
You need to use window.open(url)
instead of window.location(url)
.
window.location(url)
意思是:设置当前标签页的url.
window.location(url)
means: setting the url of the current tab.
参考:https://developer.mozilla.org/en-US/docs/Web/API/Window/open
示例:(不适用于 stackoverflow 导致沙盒框架)
Example: (doesn't work on stackoverflow cause sandboxed frame)
var randomlinks = [];
randomlinks[0]="http://ducksarethebest.com/";
randomlinks[1]="http://cant-not-tweet-this.com/";
randomlinks[2]="http://just-shower-thoughts.tumblr.com/";
randomlinks[3]="http://www.fallingfalling.com/";
randomlinks[4]="http://www.partridgegetslucky.com/";
randomlinks[5]="http://ducksarethebest.com/";
randomlinks[6]="http://cant-not-tweet-this.com/";
randomlinks[7]="http://just-shower-thoughts.tumblr.com/";
randomlinks[8]="http://www.fallingfalling.com/";
randomlinks[9]="http://www.staggeringbeauty.com/";
randomlinks[10]="http://www.trypap.com/";
function randomlink(){
window.open(randomlinks[Math.floor(Math.random()*randomlinks.length)]);
}
<form method="post">
<p><input type="button" name="B1" value="Random Link >>" onclick="randomlink()"></p>
</form>
or
<a href="#" onclick="randomlink()">Random Link</a>
这篇关于如何在新选项卡中的随机链接中打开 HTML 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文