简体   繁体   English

链接到多个网站的按钮

[英]Button that links to mulitple websites

I'm trying to create a button that links to random websites like on 'The useless web'.我正在尝试创建一个链接到“无用网络”等随机网站的按钮。 I found this code on here and it's worked almost perfectly.我在这里找到了这段代码,它工作得几乎完美。 I just need help making the sites open in a new tab, at the moment they change the current window.我只需要帮助使站点在新选项卡中打开,目前他们更改了当前的 window。 Any help will be greatly appreciated.任何帮助将不胜感激。 THANKS谢谢

CURRENT:当前的:

<script type="text/javascript">
var urls = [
    "http://www.kittenwar.com/",
    'http://heeeeeeeey.com/',
    'http://cat-bounce.com/'
];

function goSomewhere() {
    var url = urls[Math.floor(Math.random()*urls.length)];
    window.location = url; // redirect
}

onClick handler assigned: onClick 处理程序分配:

<a href="#" onClick="goSomewhere(); return false;">Gimme something weird!</a>

use this function使用这个 function

 function goSomewhere() {
        var url = urls[Math.floor(Math.random()*urls.length)];
        window.open(url, '_blank')
    }

To open a page in a new tab, you should create the button like this:要在新选项卡中打开页面,您应该像这样创建按钮:

<button href='w3schools.com' target='_blank'>Click me!</button>

You can change the href tag with JavaScript.您可以使用 JavaScript 更改href标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM