2013-03-04 56 views
1

我試圖做一個魔法八球模擬,但它不適合我。 Chrome中的檢查器元素顯示沒有錯誤,所以我很困惑它爲什麼不起作用。任何建議將不勝感激。來自字符串函數的圖像

<!DOCTYPE html> 
<html lang="en"> 
<head> 

    <title>Project 4: Consistent</title> 
    <!-- This part is the function to create my magic eight ball that will randomly give a result, but for certain questions, 
    it will provide the same answer always to fool their minds. --> 

    <script> 

     var answerMap = {} 

     var images = ['eightBallYes.png', 'eightBallNo.png', 'eightBallMillionYears.png', 'eightBallAskLater.png', 'eightBallReally.png']; 

     //I actually had a little bit of difficulty with this part of the project. 
     //The answer.search method you showed us in class for some reason is not working for me. 
     //I worked with the GTF on this part 

     function eightBall() { 
      var answer = document.getElementById("answerBox").value; 
      answer = answer.toLowerCase(); 

      if (answer.search(/[r]/) > 0) { 
       var yes = '../Images/eightBallYes.png' 
       return yes; 
      } 

      if (answer.search(/[m]/) > 0) { 
       var no = '../Images/eightBallNo.png' 
       return no; 
      } 

     } 

     window.onload = alert('Welcome to my Project 4') 

    </script> 
</head> 

<body> 
<body style="background:#EEEE17"> 
    <!-- This part of the page will simulate a magic eight ball that will provide at least 4 answers. 
    When certain questions are asked, it will return the same answers. This is honestly a pretty cool project to work on. --> 

    <div style="text-align:center"> 
     <h1>Project 4: Booyah!</h1> 
     <img src="../images/eightBallTemplate.png" > 
     <h2>Magic 8-Ball Game</h2> 


     <input type="text" id="answerBox" value="Please ask a question"> 
     <input type="button" value="Submit for Magical Results" onclick='eightBall()'/> 


     <div id="myOutput"></div> 

     <hr> 

     <a href="http://pages.uoregon.edu/alans/111/CIS%20111/p4/mac.html">Old MacDonald In-Class Activity</a> 
     <br> 
     <a href="http://pages.uoregon.edu/alans/111/CIS%20111/p4/paramString.html">Parameter In-Class Activity</a> 
     <br> 
     <a href="http://pages.uoregon.edu/alans/111/CIS%20111/p4/isPrimeLight-jQuery.html">jQuery In-Class Activity</a> 
     <br> 
     <a href="http://pages.uoregon.edu/alans/111/CIS%20111/p4/string.html">String In-Class Activity</a> 

     <footer> 

      <p> 
       &copy; Copyright by Alan Sylvestre 
      </p> 
     </footer> 
    </div> 
</body> 

+0

什麼不起作用? – SLaks 2013-03-04 04:15:28

+0

它應該爲不同的結果返回不同的圖像。例如,如果答案中有r,則返回yes,如果有m,則返回no圖像。現在沒有這樣做。如果沒有r或m,我還需要添加一個if語句以允許它從數組中返回一個隨機圖像。 – 2013-03-04 04:17:30

回答

0

你沒有做你的函數的返回值什麼。

您可能希望將其分配給<img>標記的src屬性。

+0

這很有道理。我怎麼做? – 2013-03-04 04:31:37

+0

你能告訴我一個例子嗎? – 2013-03-04 04:33:15

+0

這個建議很好,但是有人能告訴我一些關於如何使用不同變量的代碼,所以我可以直觀地看到你想要解釋什麼? – 2013-03-04 04:37:51

0

我看到的問題是,當點擊按鈕並選擇了正確的值時,您正在調用eightBall函數,但您沒有對它做任何處理,例如,在myOutput div中附加一個節點。

你也有兩個身體標籤。

+0

好的。我想我明白你的意思。那麼,我如何將它附加到myOutput div中呢? – 2013-03-04 04:24:45

+0

您首先需要對myOutput的引用,您可以使用document.getElementById方法獲取該引用。然後,您可以使用document.createElement方法創建一個img節點,將所需的值分配給src屬性,最後使用myOutput.appendChild – Marcelo 2013-03-04 04:27:58

+0

的方法追加它,或者您可以更改myOutput並使其成爲img而不是div所以你只需要更新它的src屬性就像SLaks提出的那樣。 – Marcelo 2013-03-04 04:29:29