2

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

    <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>

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

4

4 回答 4

3

您需要在引号 [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))];
    }
于 2012-10-22T05:01:10.130 回答
2

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

quotes[15] = '<div id="youWishYouCouldReadThis">InsertCodeHere</div>';
于 2012-10-22T04:59:32.710 回答
1

首先,您从 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'
]

第四,您不应该在<table>. 在<p>您的情况下,标签会更好。

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

于 2012-10-22T05:13:25.530 回答
0

引号[15] 应该有双引号,或者你需要转义单引号

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

或者

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

PS你的body/div结束标签的顺序不正确

是您的代码工作。

于 2012-10-22T05:06:55.580 回答