2012-06-03 44 views
2

對不起,如果這是一個非常愚蠢的問題,我對JavaScript有點新鮮。我試圖製作一個包含多個功能的網頁,但只有第一個功能會成功。我在谷歌搜索,我只有一次調用多個函數的結果,但這不是我所期待的。這裏是我想要做的一個例子:Javascript中的多個函數

<html> 
    <head> 
     <script type="text/javascript"> 
      function frogger() 
      { 
       document.getElementById("descriptions").innerHTML="Frogger <br />Description: Get 
        the frog to the islands at the top of the screen without falling into the water or 
        getting hit by cars. <br />Controls: Up arrow key to move forward, down arrow key to 
        move backward, left arrow key to move left, and right arrow key to move right."; 
      } 
      function clear() 
      { 
       document.getElementById("descriptions").innerHTML=" "; 
      } 
     </script> 
    </head> 
    <body> 
     <div id="descriptions" style="{height:100;}"> 
     </div> 
     <div class="game" onmouseover="frogger()" onmouseout="clear()"> 
      <a href="/arcade/frogger.html"><img border="0" src="http://ggmedia.site50.net 
/pics/frogger.PNG" height="100" width="100" /><br />Frogger</a> 
     </div> 
    </body> 
</html> 

感謝您的幫助!

+3

你不能把換行符在這樣的字符串。 – Pointy

+1

檢查您的JS控制檯以查看錯誤。 –

+1

您正在錯誤地使用樣式屬性。而不是'style =「{height:100;}」'do'style =「height:100;」 – j08691

回答

5

已經有一個在document對象命名爲clear功能明確的方法。爲其他功能命名。

2

你的字符串中有換行符,您可以將其刪除或添加\到每一行的末尾

function frogger() 
{ 
    document.getElementById("descriptions").innerHTML="Frogger <br />Description: Get\ 
the frog to the islands at the top of the screen without falling into the water or\ 
getting hit by cars. <br />Controls: Up arrow key to move forward, down arrow key to\ 
move backward, left arrow key to move left, and right arrow key to move right."; 
} 

編輯:如果你改變了clear函數的名稱說clearx它的工作原理,奇怪的。

編輯:顯然有在文檔對象

+0

加入'\'會發生什麼,將上面的句子變成單個句子嗎? –

2
function frogger() { 
    document.getElementById("descriptions").innerHTML="Frogger <br />Description: Get the frog to the islands at the top of the screen without falling into the water or getting hit by cars. <br />Controls: Up arrow key to move forward, down arrow key to move backward, left arrow key to move left, and right arrow key to move right."; 
} 
+0

嗯,我真的更喜歡看到連續線或連字符而不是長屁股字符串。 –

+0

@sachleen我從來沒有見過像這樣的字符串的反斜槓方法。謝謝。該方法是否有名字? –

+0

無論如何,我會+1,因爲每行5000+字符的字符串只供閣下閱讀。 –

0

clear()函數重命名爲其他內容。我將它更改爲clearDesc()並且它工作正常(在修復了字符串中的換行符問題之後)。見here

0
<div class="game" onmouseover="frogger()" onmouseout="clearr()">mousee</div> 
    <div id="descriptions"></div> 
    <script type="text/javascript"> 
     var frogger = function() { 
      this.innerHTML = ["Frogger <br />Description: Get}", 
        "the frog to the islands at the top of the screen without falling into the water or", 
        "getting hit by cars. <br />Controls: Up arrow key to move forward, down arrow key to", 
        "move backward, left arrow key to move left, and right arrow key to move right."].join(''); 
     }.bind(document.getElementById("descriptions")); 
     // 
     var clearr = function() { 
      this.innerHTML = " "; 
     }.bind(document.getElementById("descriptions")); 

    </script> 

下面是該代碼在jsfiddle.net

http://jsfiddle.net/gerst20051/6Neqv/