【问题标题】:Can't get my function to act properly无法让我的功能正常运行
【发布时间】: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


【解决方案1】:

两件事:

在 HTML 中更改这一行

<button type="button" onclick="makeSong">

<button type="button" onclick="makeSong();">

makeSong函数开头js中的错字:

ouput=document.getElementById("output");

tput

缺少“t”

【讨论】:

  • 谢谢!天哪,我觉得我永远也弄不明白。我一遍又一遍地重复它。我很感激帮助!!!你们是最好的响应性论坛。
猜你喜欢
  • 2015-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-24
  • 2011-08-21
  • 1970-01-01
相关资源
最近更新 更多