【问题标题】:Generating a random string with this HTML/Javascript/CSS?用这个 HTML/Javascript/CSS 生成一个随机字符串?
【发布时间】:2012-02-20 22:32:08
【问题描述】:

我已经用这个 HTML 和 javascript 摆弄了一两个小时了......我不知道为什么它不起作用。我一直在尝试自己学习 html、css 和 javascript……但我认为 Eclipse 不能很好地调试我的东西……这是怎么回事?

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<style type="text/css">
a:link {color:#FF0000;}    /* unvisited link */
a:visited {color:#00FF00;} /* visited link */
a:hover {color:#FF00FF;}   /* mouse over link */
a:active {color:#0000FF;}  /* selected link */
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Catlard.com</title>

<link rel=StyleSheet href="styles/menuStyle.css" type="text/css"/>

<script type="text/javascript">

function RandomQuote()
{
    var quotes= new Array();
    quotes[0] = "...believes it may be useful in a time of need."
    quotes[1] = "...knows you have a problem, but accepts you anyway."
    quotes[2] = "...believes the aliens were involved at Oak Island."
    quotes[3] = "...demands to know the location of your hidden rebel base!"
    quotes[4] = "...SAW you take the cookie from the cookie jar."
    return quotes[Math.floor(Math.random() * 4.99);
}

</script>

</head>
<body>

<div id="framecontent">
<div class="innertube">
<h1>CSS Top Frame Layout</h1>
<span style="font-family : Courier;color: #000000;">
<a href="resume.html"> Resume </a>
<a href="http://catlard.blogspot.com"> Blog </a>
<a href="pixelating.html"> Arts n' Farts</a>
<a href="contact.html"> Contact </a>
<a href="typing.html"> Games </a>
</span>
</div>
</div>


<div id="maincontent">
<div class="innertube">

document.write(RandomQuote());
<p style="text-align: center">Blah blah blah </p>
</div>
</div>

</body>
</html>

【问题讨论】:

  • 您没有告诉我们运行此程序时会发生什么。有什么问题?
  • 啊,对不起。当我运行它时,它在我调用该函数的浏览器中不显示任何内容。
  • 因为你没有调用你的代码。它被格式化为文本。尝试将其放入另一个脚本标签中。 -1 表示缺少 r
  • 缺少 r-?对不起,我有点困惑。
  • +1 表示勇敢但徒劳地尝试通过添加神秘的“r”来修复。

标签: javascript html css string random


【解决方案1】:

返回引号行有错误。它应该以 )] 结尾;不是);

此外,您的每一行引号 [n] 都应以分号结尾。

而且,您应该在script 标签中包含 document.write()。

【讨论】:

  • 啊,这完全正确。我做了所有这些事情,现在它正在工作。我为我所有的语法错误道歉——我没有一个很好的 IDE——我现在正在使用 Eclipse for Javascript。对 OSX 32 位初学者有什么建议吗?
【解决方案2】:

您的“document.write”行需要位于&lt;script&gt; 区域中。您还缺少返回行上的“]”括号。

【讨论】:

    【解决方案3】:

    您需要更清楚地检查您的代码。或许可以使用 SciTe 等语法高亮编程编辑器。

    即使你没有明确告诉我们你遇到的问题(只是说它不起作用),我首先想到的是这行:

    return quotes[Math.floor(Math.random() * 4.99);
    

    您忘记了数组的结尾 ]

    【讨论】:

    • 啊,是的,没错。非常感谢。我将检查 SciTe,而不是 eclipse。干杯!
    【解决方案4】:

    return 命令中缺少结尾的 ]。

    【讨论】:

    • 谢谢!这是一个大问题,除其他外。
    【解决方案5】:

    两件事:

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

    您缺少引号数组的结束标记。

    <script language="javascript" type="text/javascript">
        document.write(RandomQuote());
    </script>
    

    您的 javascript 必须包含在该脚本标记中,否则它只会呈现为 HTML。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 2011-07-23
      • 2014-03-26
      • 2010-11-23
      • 1970-01-01
      相关资源
      最近更新 更多