【问题标题】:How do I make a HTML button open in random links in new tab如何在新选项卡中的随机链接中打开 HTML 按钮
【发布时间】:2016-04-30 15:43:24
【问题描述】:

所以我正在创建一个无用的网站,我需要制作一个按钮,将人们引导到新标签中的随机网站。我已经有了随机链接按钮代码,但我似乎无法使用新标签代码添加它。

样本

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>

【问题讨论】:

    标签: html button tabs


    【解决方案1】:

    您需要使用window.open(url) 而不是window.location(url)

    window.location(url) 表示:设置当前标签页的url。

    参考:https://developer.mozilla.org/en-US/docs/Web/API/Window/open

    示例:(不适用于stackoverflow导致沙盒框架)

    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>

    【讨论】:

    • 那么你能告诉我我应该使用和添加的完整代码吗?
    猜你喜欢
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 2022-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多