【问题标题】:Trying to set up randomized text. What's wrong with my code?尝试设置随机文本。我的代码有什么问题?
【发布时间】:2012-10-22 04:55:44
【问题描述】:

我正在尝试设置一些内容,该内容实质上是从一组预定义的短语中进行选择,并将其作为子标题显示在我的页面上。我在这里做错了什么?

    <html>
    <head>
    <script type="text/javascript">
        function RandomQuote()
        {
        var quotes= new Array();
        quotes[0] = 'Home of Fall!'
        quotes[1] = 'At Least This is Something!'
        quotes[2] = 'Javascript!'
        quotes[3] = 'Railroads at Last!'
        quotes[4] = 'Better than Wikipedia'
        quotes[5] = 'Now with Scorpions!'
        quotes[6] = 'Irrelevant Stuff!'
        quotes[7] = 'Fall is here!'
        quotes[8] = 'That moment when Image-908329490283094.jpg'
        quotes[9] = 'With Added Roundness!'
        quotes[10] = 'Bacon!'
        quotes[11] = 'Try Out WinterNstuff!'
        quotes[12] = 'Try Out SpringNstuff!'
        quotes[13] = 'Try Out SummerNstuff!'
        quotes[14] = 'all html is lowercase!'
        quotes[15] = '<div id=''youWishYouCouldReadThis''>InsertCodeHere</div>'
        quotes[16] = 'In Remembrance of the soldiers who died in the Iranian War'
        quotes[17] = 'Leaf Me Alone!'
        quotes[18] = 'Pumpkins Were So 2011.'
        quotes[19] = 'This Does not Count as a Random String'
        quotes[20] = 'With Added Content Generation!'

    return quotes[Math.floor((Math.random() * 19.9999))];
}
    </script>
    <title></title>
    </head>
    <body>
    <div id="container">
    <div id="header">
    <table style="background-color:blue">
    String:
    <script type="text/javascript">document.write(RandomQuote());</script>
    </table>
    </body>
</div>
</div>

作为后续,有没有办法配置标题以使用随机字符串生成做同样的事情?

【问题讨论】:

  • 我不明白你的后续想法。你想用标题做什么?你想把随机字符串生成的值放在标题中吗?
  • @Ranjith 是的,这正是我想要做的。标题中可以调用函数和脚本吗?
  • 不。您需要做的是从脚本标签中调用它。并将值附加到标题。使用 JQuery,因为它非常适合这样的东西。看这里的例子:jsfiddle.net/ranjith19/HCCSW/4 你想要的是所谓的 DOM 操作。阅读它

标签: javascript random


【解决方案1】:

您需要在引号 [15] 中转义引号。这是一个工作小提琴http://jsfiddle.net/ranjith19/HCCSW/

function RandomQuote()
    {
    var quotes= new Array();
    quotes[0] = 'Home of Fall!'
    quotes[1] = 'At Least This is Something!'
    quotes[2] = 'Javascript!'
    quotes[3] = 'Railroads at Last!'
    quotes[4] = 'Better than Wikipedia'
    quotes[5] = 'Now with Scorpions!'
    quotes[6] = 'Irrelevant Stuff!'
    quotes[7] = 'Fall is here!'
    quotes[8] = 'That moment when Image-908329490283094.jpg'
    quotes[9] = 'With Added Roundness!'
    quotes[10] = 'Bacon!'
    quotes[11] = 'Try Out WinterNstuff!'
    quotes[12] = 'Try Out SpringNstuff!'
    quotes[13] = 'Try Out SummerNstuff!'
    quotes[14] = 'all html is lowercase!'
    quotes[15] = '<div id=\'youWishYouCouldReadThis\'>InsertCodeHere</div>'
    quotes[16] = 'In Remembrance of the soldiers who died in the Iranian War'
    quotes[17] = 'Leaf Me Alone!'
    quotes[18] = 'Pumpkins Were So 2011.'
    quotes[19] = 'This Does not Count as a Random String'
    quotes[20] = 'With Added Content Generation!'

return quotes[Math.floor((Math.random() * 19.9999))];
    }

【讨论】:

  • 我的问题是它没有显示任何字符串。即使在完全删除引用 #15 之后,它似乎也不起作用。想法?
  • 您需要从某个地方调用该函数。你已经创建了一个函数,现在你需要从某个地方调用它
【解决方案2】:

引用 #15 应该有正确的引用:

quotes[15] = '<div id="youWishYouCouldReadThis">InsertCodeHere</div>';

【讨论】:

    【解决方案3】:

    首先,您从 0..19.999 生成一个索引,因此永远不会使用 quotes[20]

    最好的解决办法是

    return quotes[Math.random()*quotes.length];
    

    其次,正如其他人所提到的,引用 #15 没有正确转义:

    quotes[15] = '<div id=''youWishYouCouldReadThis''>InsertCodeHere</div>'
    

    应该是

    quotes[15] = '<div id=\'youWishYouCouldReadThis\'>InsertCodeHere</div>'
    

    quotes[15] = '<div id="youWishYouCouldReadThis">InsertCodeHere</div>'
    

    第三,创建固定数组有更简单的方法:

    quotes = [
      'Home of Fall!',
      'At least This is Something!',
      ...
      'With Added Content Generation'
    ]
    

    第四,您不应该在&lt;table&gt; 中放置纯文本。在您的情况下,&lt;p&gt; 标签会更好。

    第五,您的结束标签的顺序不正确。感谢@rlay3 注意到这一点。

    【讨论】:

      【解决方案4】:

      quotes[15] 应该有双引号,否则你需要转义单引号

      quotes[15] = '<div id=\'youWishYouCouldReadThis\'>InsertCodeHere</div>'
      

      quotes[15] = '<div id="youWishYouCouldReadThis">InsertCodeHere</div>'
      

      附:您的 body/div 结束标签的顺序不正确

      Here 是你的代码工作。

      【讨论】:

      • 如需跟进,请查看示例here
      • 还可以查看@Jan Dvorak 对代码改进的建议
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      • 2014-09-06
      • 1970-01-01
      • 2011-07-28
      相关资源
      最近更新 更多