随机口号发生器使用javascript的开关 [英] Random slogan generator with using javascript's switch

查看:87
本文介绍了随机口号发生器使用javascript的开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有什么问题。我正在学习javascript,我想使用开关为我的网站创建随机标语生成器。

I have got little problem. I'm learning javascript and I wanted to create random slogan generator for my site with using switch.

所以我创建了这个HTML代码

So I created this html code

<body onload="rndqu()">
    <div id="head"> <a href="index.html">Mira's place<a><br>
            <h2>&#8220;<span id="quote"></span>&#8221;</h2>
    </div>
</body>

并使用此Javascript

and used this Javascript

var qu;
var slogan;
function rndqu(n){
    var random = function(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
    };  
    qu = random(1, 3);
}
switch(qu){
    case 1:
        slogan = "Here is the 1";
        break;
    case 2:
        slogan = "Here is the 2";
        break;
    case 3:
        slogan = "Woah";
        break;
    default:
        slogan = "Really?";
}
document.getElementById("quote").innerHTML = slogan;

我不明白为什么它不起作用。有人能帮我吗?谢谢!这里有jsfiddle http://jsfiddle.net/NX3cz/

I don't get it why it doesn't work. Can someone help me? Thank you! And here is jsfiddle of it http://jsfiddle.net/NX3cz/

推荐答案

您将部分代码留在 rndqu()函数之外。
我在这里分叉并纠正了你的小提琴:
http://jsfiddle.net/BwJ7s/

You left part of the code outside the rndqu() function. I forked and corrected your fiddle here: http://jsfiddle.net/BwJ7s/

以下是更正的JS代码:

Here's the corrected JS code:

var qu;
var slogan;
function rndqu(n)
{
    var random = function(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
    };  
    qu = random(1, 3);

    switch(qu){
        case 1:
            slogan = "Here is the 1";
            break;
        case 2:
            slogan = "Here is the 2";
            break;
        case 3:
            slogan = "Woah";
            break;
        default:
            slogan = "Really?";
    }
    document.getElementById("quote").innerHTML = slogan;
}

这篇关于随机口号发生器使用javascript的开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆