【发布时间】:2015-04-15 14:08:33
【问题描述】:
好的,所以我很难处理无法与我的其余代码一起使用的函数。我检查了拼写错误,尝试了调试,等等。我的按钮 (makeSong) 应该运行带有诗句和合唱的歌曲“Ants go marching down”,但是当我按下页面上的按钮时,它就保持不变。为什么会这样?函数一直给我带来非常困难的时期。我希望我能很好地解释我的问题,这只是我的第二个问题,我是一个自学成才的新手。因此,如果你们可以看一下并解释为什么我的代码不正确。我是否将功能放在错误的位置?我很困惑。
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8"></meta>
<title>AntsFunction.html</title>
<script type="text/javascript">
// from AntsFunction.html
var output;
function chorus(){
var text= "...and they all go marching down<br/>";
text += "to the ground<br/>";
text += "to get out<br/>";
text += "of the rain.<br/>";
text += "<br/>";
text += "boom boom boom boom boom boom boom boom<br/><br/>";
output.innerHTML += text;
} // end chorus
function verse1(){
var text= "The ants go marching 1 by 1 hurrah, hurrah<br/>";
text += "The ants go marching 1 by 1 hurrah, hurrah<br/>";
text += "The ants go marching 1 by 1<br/>";
text += "The little one stops to suck his thumb<br/><br/>";
output.innerHTML += text;
} // end verse1
function verse2(){
var text= "The ants go marching 2 by 2 hurrah, hurrah<br/>";
text += "The ants go marching 2 by 2 hurrah, hurrah<br/>";
text += "The ants go marching 2 by 2<br/>";
text += "The little one stops to tie his shoe<br/><br/>";
output.innerHTML += text;
} // end verse2
function makeSong(){
ouput=document.getElementById("output");
output.innerHTML="";
verse1();
chorus();
verse2();
chorus();
} // end makeSong
</script>
</head>
<body>
<h1>Using Basic Functions</h1>
<form action="">
<fieldset>
<button type="button"
onclick="makeSong">
Make Song
</button>
</fieldset>
</form>
<div id="output">
The song will appear here...
</div>
</body>
</html>
【问题讨论】:
-
在 onclick 属性中尝试
makeSong()。 -
另外
#output可能会影响全局output变量。 -
提示:您的拼写错误检查不成功。查看您的
makeSong函数。 -
我尝试了以上所有方法,但没有任何效果。你在说我的拼写错误在哪里?我看过了,没看到。
标签: javascript function button placement