[英]Get a Single value of Li randomly and print it in <h1> tag
I have a list of values in a ul element in HTML and using javascript, I want to output in h1 a random value out of that list. 我在HTML的ul元素中有一个值列表,并且使用javascript,我想在h1中从该列表中输出一个随机值。 I already have a script but its inputting the ID of that LI instead of the value.
我已经有一个脚本,但是它输入的是该LI的ID而不是值。
Here's my HTML: 这是我的HTML:
<div id="quotes-list">
<ul>
<li id="One">One</li>
<li id="Two">Two</li>
<li id="Three">Three</li>
<li id="Four">Four</li>
<li id="Five">Five</li>
</ul>
</div>
<h1 id="quoted"></h1>
and here's my javascript: 这是我的JavaScript:
var lis = document.getElementById("quotes-list").getElementsByTagName("li");
var countThis = Math.floor(Math.random() * lis.length);
var carToon = (lis[countThis].id);
var textElement = document.getElementById("quoted");
textElement.innerHTML = carToon;
As I've said, this is working but it's outputting the id of the LI. 就像我说的那样,这是可行的,但是它正在输出LI的ID。
I've tried playing around with the 我试过了
var carToon = (lis[countThis].id);
putting lis[countThis]. 放置lis [countThis]。 text but the output would be " UNDEFINED "
文字,但输出将是“ UNDEFINED ”
What could be replace on my code so that I can output in H1 a random Value out of my ul list? 在我的代码上可以替换什么,以便可以在H1中从ul列表中输出随机值?
Change this line 更改此行
var carToon = (lis[countThis].id);
to 至
var carToon = (lis[countThis].innerHTML);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.