如何使用 JavaScript 在网站上生成随机报价?

How to use JavaScript to generate random quotes on a website?

下面是我目前拥有的代码。我遇到的问题是引号应该显示在网页上的地方,它只是说 "undefined"。老实说,我不确定问题出在哪里。

var randomQ = randomInt(0, 10);

function randomInt(lowest, size) {
    Math.floor(Math.random() * size) + lowest;
    return randomQ;
}

var quoteElem = document.getElementsByTagName("quote")[0];

quoteElem.innerHTML = getQuote(randomQ);

function getQuote(n) {
   var quotes = [
   "It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.",
   "I hate to hear you talk about all women as if they were fine ladies instead of rational creatures. None of us want to be in calm waters all our lives.",
   "Silly things do cease to be silly if they are done by sensible people in an impudent way.",
   "Give a girl an education and introduce her properly into the world, and ten to one but she has the means of settling well, without further expense to anybody.",
   "Life seems but a quick succession of busy nothings.",
   "Our scars make us know that our past was for real.",
   "I cannot speak well enough to be unintelligible.",
   "One cannot be always laughing at a man without now and then stumbling on something witty.",
   "Men were put into the world to teach women the law of compromise.",
   "The person, be it gentlemen or lady, who has not pleasure in a good novel, must be intolerably stupid."
   ];
   
   return quotes[n];
}

你应该学会阅读你的错误。 undefined什么?

"message": "Uncaught TypeError: Cannot set property 'innerHTML'

这意味着您正在尝试设置一个不能 found/doesn 不存在的元素的内部HTML。

var quoteElem = document.getElementsByTagName("quote")[0];

在HTML中,没有名为'quote'的元素标签。也许您的意思是 ID 为 'quote' 的元素?

其次,您名为 randomInt() 的函数并未返回您生成的随机数,而是返回一些名为 'randomQ'

的未定义变量

var randomQ = randomInt(0, 10);
function randomInt(lowest, size) {
  //Return the actual value instead
  return Math.floor(Math.random() * size) + lowest; 

  //return randomQ  <-- what is this? This is what is undefined
}

var quoteElem = document.getElementById("quote");

quoteElem.innerHTML = getQuote(randomQ);

function getQuote(n) {
   var quotes = [
   "It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.",
   "I hate to hear you talk about all women as if they were fine ladies instead of rational creatures. None of us want to be in calm waters all our lives.",
   "Silly things do cease to be silly if they are done by sensible people in an impudent way.",
   "Give a girl an education and introduce her properly into the world, and ten to one but she has the means of settling well, without further expense to anybody.",
   "Life seems but a quick succession of busy nothings.",
   "Our scars make us know that our past was for real.",
   "I cannot speak well enough to be unintelligible.",
   "One cannot be always laughing at a man without now and then stumbling on something witty.",
   "Men were put into the world to teach women the law of compromise.",
   "The person, be it gentlemen or lady, who has not pleasure in a good novel, must be intolerably stupid."
   ];
   
   return quotes[n];
}
<div id="quote"></div>