【问题标题】:How to use JavaScript to generate random quotes on a website?如何使用 JavaScript 在网站上生成随机引号?
【发布时间】:2019-04-14 18:07:50
【问题描述】:

以下是我目前拥有的代码。我遇到的问题是应该在网页上显示引号的位置,它只是说“未定义”。老实说,我不确定问题出在哪里。

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];
}

【问题讨论】:

  • 在此处发布您的 html...
  • 绝对需要你的 html 才能提供帮助..

标签: javascript quotes


【解决方案1】:

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

“消息”:“未捕获的类型错误:无法设置属性 'innerHTML'

这意味着您正在尝试设置无法找到/不存在的元素的 innerHTML。

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

在 HTML 中,没有称为“引号”的元素标签。也许您的意思是 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];
}
&lt;div id="quote"&gt;&lt;/div&gt;

【讨论】:

    猜你喜欢
    • 2016-12-28
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    相关资源
    最近更新 更多