【发布时间】:2022-01-01 17:33:56
【问题描述】:
如何让js通过单击按钮从列表中显示随机单词并将其打印在标题3中?我需要快速回复。请尽快回复
//Js代码开始
let display = document.getElementById("motivato");
console.log(display)
var motivation = [{
quote: "Planting popcorn does not produce more popcorn",
person: "Farmer Ted"
}, {
quote: "White whale, bad whale",
person: "Confucious (Moby Dick)"
}, {
quote: "Use the strobe function to disorientate your attacker",
person: "Flashlight"
}, {
quote: "Apply liberally to your erogenous zones",
person: "Spice Bomb"
}, {
quote: "Help me, I'm bleaching",
person: "The Great Barrier Reef"
}];
function motivateMe() {
var listLength = Object.keys(motivation).length;
var randVal = Math.floor(Math.random() * listLength);
document.write(motivation[randVal]);
display.innerHTML = motivation;
}
请编辑我的代码并在下方回复
谢谢
【问题讨论】:
-
澄清一下,您是要从对象中打印随机词还是随机对象?
-
您可能不想使用 document.write(除非您真的想全部写出来)。为什么下面的行试图将整个动机放入innerHTML而不是选定的引用?
标签: javascript html css bootstrap-4