简体   繁体   English

通过单击按钮从数组内的对象获取随机值

[英]Getting random values from objects inside an array by clicking a button

I need to a get a random string from the objects inside an array when you click the button. 当您单击按钮时,我需要从数组内的对象中获取随机字符串。 But for some reason my code is not executing. 但是由于某种原因我的代码没有执行。

 var quotes = [{ quote: "Quote1", source: "Source1" }, { quote: "Quote2", source: "Source2" }, { quote: "Quote3", source: "Source3" }, { quote: "Quote4", source: "Source4" }, { quote: "Quote5", source: "Source5" } ]; function getRandomquote() { var randomindex = Math.floor(Math.random() * (quotes.length)); var quotesarr = quotes[randomindex]; var objquote = quotesarr.quote; var objsource = quotesarr.source; document.getElementById("quote").innerHTML = objquote; document.getElementById("source").innerHTML = objsource; } function printQuote() { document.getElementById("loadQuote").onclick = getRandomquote; } printQuote(); 
 <div class="container"> <div id="quote-box"> <p class="quote"> hello</p> <p class="source"> hello</p> </div> <button id="loadQuote" onclick="printQuote();">Show another quote</button> 

I am getting this error message: 我收到此错误消息:

Uncaught TypeError: Cannot set property 'innerHTML' of null at HTMLButtonElement.getRandomquote (randomtest1.js:27) 未捕获的TypeError:无法在HTMLButtonElement.getRandomquote上将属性'innerHTML'设置为null(randomtest1.js:27)

Update after answers below 下面的答案后更新

I changed getElementById to getElementsByClassName , and now there are no error messages. 我将getElementById更改为getElementsByClassName ,现在没有错误消息。

But when I click the button, it does not change the elements. 但是,当我单击按钮时,它不会更改元素。 I believe I have made a mistake on the printQuote function. 我相信我在printQuote函数上犯了一个错误。 I cannot figure it out. 我想不明白。

You can't use getElementById to retrieve an element by its class . 您不能使用getElementById来按其class检索元素。 You'll need to specify an id or use getElementsByClassName - https://developer.mozilla.org/en/docs/Web/API/Document/getElementsByClassName 您需要指定一个id或使用getElementsByClassName - https://developer.mozilla.org/en/docs/Web/API/Document/getElementsByClassName

 var els = document.getElementsByClassName('test') console.log(els.length) 
 <div class="test"></div> 

In your example: 在您的示例中:

<p class="quote" id="quote"> hello</p>
<p class="source" id="source"> hello</p>

You'll need to add the ids to the elements as below. 您需要将id添加到元素,如下所示。 Or use getElementsByClassName() or use querySelector(".quote") . 或使用getElementsByClassName()或使用querySelector(".quote")

 var objquote = "Hello"; var objsource = "World"; document.getElementById("quote").innerHTML=objquote; document.getElementById("source").innerHTML=objsource; 
 <div id="quote-box"> <p id="quote" class="quote"> hello</p> <p id="source" class="source"> hello</p> </div> 

Please find the working code below. 请在下面找到工作代码。 i have removed unwanted functions. 我已经删除了不需要的功能。

html html

<div id="quote-box">
  <p class="quote">hello</p>
  <p class="source">hello</p>
</div>
<button id="loadQuote" onclick="getRandomquote();">Show another quote</button>

js JS

var quotes=[
{quote:"Quote1", source:"Source1"},
{quote:"Quote2", source:"Source2"},
{quote:"Quote3", source:"Source3"},
{quote:"Quote4", source:"Source4"},
{quote:"Quote5", source:"Source5"}
        ];
getRandomquote=function(){

var randomindex=Math.floor(Math.random()*(quotes.length));  
var quotesarr=quotes[randomindex];
document.getElementsByClassName("quote")[0].innerHTML=quotesarr.quote;
document.getElementsByClassName("source")[0].innerHTML=quotesarr.source;
}

您尚未在p标签中设置id ...请设置id ='quote'并且它应该开始工作。

I changed getElementById to getElementsByClassName, and now there are no error messages. 我将getElementById更改为getElementsByClassName,现在没有错误消息。

But when I click the button, it does not change the elements. 但是,当我单击按钮时,它不会更改元素。 I believe I have made a mistake on the printQuote function. 我相信我在printQuote函数上犯了一个错误。 I cannot figure it out. 我想不明白。

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

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