gpt4 book ai didi

javascript - newQuote 未定义,不确定原因

转载 作者:行者123 更新时间:2023-12-03 08:44:12 26 4
gpt4 key购买 nike

为我调试的另一个代码必须保持相同,因为无法重写。但我想知道为什么 newQuote 会吐出未定义的错误。我还相信我需要修复计时器设置以使其显示,因为 var tick 没有被调用,但不能 100% 确定是否有任何建议受到赞赏,因为除了 newQuote undefined 之外我找不到任何错误

   <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta charset="utf-8" />
<title>Random Proverbs</title>
<script type="text/javascript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
function changeQuote() {
quotes = new Array;
quotes[0] = "Laughter is the best medicine.";
quotes[1] = "Never look a gift horse in the mouth.";
quotes[2] = "One good turn deserves another.";
quotes[3] = "The early bird catches the worm.";
quotes[4] = "Two is company, three is a crowd.";
var newQuote = quotes[Math.round(Math.random()+quotes.length)];
document.quoteform.quote.value = newQuote;
}
var tick = setInterval(changeQuote(), 1000); //missing time in milliseconds and double quotes not needed
// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>
</head>
<body>
<form id="quoteform" action=""> <!--Was -->
<input type="text" size="50" id="quote" name="quote" /><br />
</form>
</body>
</html>

最佳答案

将 document.quoteform.quote 更改为 document.forms.quoteform.quote然后将表单命名为 quoteform所以新的 JavaScript 看起来像

function changeQuote() {
var quotes = new Array; //was var defintion
quotes[0] = "Laughter is the best medicine.";
quotes[1] = "Never look a gift horse in the mouth.";
quotes[2] = "One good turn deserves another.";
quotes[3] = "The early bird catches the worm.";
quotes[4] = "Two is company, three is a crowd.";
var newQuote = quotes[Math.floor(Math.random()*quotes.length)];
document.forms.quoteform.quote.value = newQuote;
}
setInterval(changeQuote, 1000);

那么您只需将开始表单标记中的 id 更改为 name

关于javascript - newQuote 未定义,不确定原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32958908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com