简体   繁体   English

第一次单击后Javascript onclick事件不起作用-随机报价生成器

[英]Javascript onclick event doesn't work after the first click - random quote generator

I'm building a random quote generator, when I click the 'get a quote button' it generates one of the quotes randomly, but after the first click it doesn't provide more sentences when clicked again. 我正在构建一个随机报价生成器,当我单击“获取报价按钮”时,它会随机生成其中一个报价,但是第一次单击后,再次单击时它不会提供更多的句子。

here is my code: 这是我的代码:

html: 的HTML:

<h1>Random Quotes</h1>
<p>Press The below button to read a random quote</p>

<p id="demo"></p>
<div class="button" onclick="loopQuotes()">Get a quote</div>
<script type="text/javascript">

</script>

javascript: javascript:

var quotes = [("Quote 1"), ('quote 2'), ('quote 3'), ('quote 4')];

var length = quotes.length;
var rand = Math.round(Math.random() * (length - 1));

function loopQuotes(){
    for (var i = 0; i < quotes.length; i++ ) {
        document.getElementById('demo').innerHTML = quotes[rand];
    }
} 

Can someone kindly check and advise? 有人可以检查并提出建议吗?

Many thanks in advance 提前谢谢了

As someone has already pointed out, your rand value is not getting generated again. 正如有人已经指出的那样,您的rand价值不会再产生。 Since you call loopQuotes methods which doesn't have the logic of generating rand again. 由于调用了loopQuotes方法,因此没有再次生成rand的逻辑。

Try 尝试

function loopQuotes()
{
  var rand = Math.round(Math.random() * (length - 1));
  document.getElementById('demo').innerHTML = quotes[rand];
}

Try this way , because you have calling Random outside of function. 尝试这种方式,因为您已经在函数外部调用Random了。

function loopQuotes(){
    var length = quotes.length;
    var rand = Math.round(Math.random() * (length - 1));
    document.getElementById('demo').innerHTML = quotes[rand];
}

replace your code with below 用下面的代码替换

var quotes = ["Quote 1", 'quote 2', 'quote 3', 'quote 4'];
var length = quotes.length;
function loopQuotes()
{
    for (var i = 0; i < quotes.length; i++ ) 
    {
        var rand = Math.round(Math.random() * (length - 1));
         document.getElementById('demo').innerHTML = quotes[rand];
    }
}

It's because you are generating your random variable outside of your function. 这是因为您正在函数外生成随机变量。 Whenever you put JavaScript into an HTML page, it gets run exactly once, and only the stuff inside function s is available to be run at a later time. 每当您将JavaScript放入HTML页面时,它就只会运行一次,只有function s中的内容才可以在以后运行。 Therefore, you should put your assignment to rand inside your function body: 因此,您应该rand的赋值放在函数体内:

function loopQuotes(){
    var rand = Math.round(Math.random() * (quotes.length - 1));
    for (var i = 0; i < quotes.length; i++ ) {
        document.getElementById('demo').innerHTML = quotes[rand];
    }
}

In fact, it makes no sense to loop since rand isn't changing, so you can get rid of the for loop and just keep what's inside. 实际上,由于rand不变,因此循环是没有意义的,因此您可以摆脱for循环而只保留其中的内容。 If you meant to generate a big, long quote pieced together from multiple quotes, you'll want to do something like this in which you put the rand generation inside the loop body and add to the innerHTML string instead of replacing it: 如果要生成一个由多个引号拼凑而成的大而长的引号,则需要执行以下操作:将rand生成放入循环主体中,并添加到innerHTML字符串中,而不是替换它:

function loopQuotes(){
    document.getElementById('demo').innerHTML = "";
    var length = quotes.length;
    for (var i = 0; i < length; i++ ) {
        var rand = Math.round(Math.random() * (quotes.length - 1));
        document.getElementById('demo').innerHTML += quotes[rand];
    }
}

Updated Fiddle - https://jsfiddle.net/3wuyo60p/ 更新的小提琴-https: //jsfiddle.net/3wuyo60p/

Firstly you need to generate numbers between 0 to length-1. 首先,您需要生成0到length-1之间的数字。

Secondly you dont need looping, if you plan to generate a number every time the "Get a Quote" div is clicked. 其次,如果您计划每次单击“获取报价” div时生成一个数字,则不需要循环。

<script type="text/javascript">
var quotes = [("Quote 1"), ('quote 2'), ('quote 3'), ('quote 4')];

function loopQuotes(){ 
var length = quotes.length; 
var max = quotes.length - 1;
var min = 0;
var rand = Math.round(Math.random() * (max - min) + min );

//console.log(rand);
document.getElementById('demo').innerHTML = quotes[rand]; 

}
</script>

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

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