2013-03-27 43 views
0

這是我的編碼..我做了什麼錯誤。我在6號,它不工作有人可以看看這個,給我一些幫助。? 感謝首頁工作幫助我被困在NUmber 6 Window.prompt()步驟

<html> 
    <head> 
    <title> My Homework </title> 
    </head> 
    <body> 
    <div style="width:400px; height:200px" id="firstdiv"> 
    <br /> <br /><center>Well Hi there! <br/> Answer the prompt to see what happens next.</center> 
    </div> 

    <script type="text/javascript"> 
    var moquestion = window.prompt("What is the capital of Missouri?",""); 

    if (moquestion.length < 1) { 
     <div style="width:400px; height:200px" id="sorrydiv"> 
      <br /> <br /> <h1><center>Sorry you don't feel like playing.<br />The capital of Missouri is Jefferson City</center></h1> 
     </div> 
     } 
    </script> 

    </body> 
</html> 

下面是分配

  1. 創建所有基本的HTML標記的網頁。
  2. <body>的內部,使用元素ID創建一個<div>標籤。
  3. 將一些文字插入<div>標籤。
  4. 將此腳本添加到此<div>以下,以便它不會嘗試運行,直到已加載。
  5. 使用window.prompt方法詢問「什麼是密蘇里州的首府?」確保沒有默認答案顯示給用戶。
  6. 檢查以確保返回的字符串的長度屬性不小於1.如果爲空,則在<div>標籤中寫下如下內容:「對不起,您不喜歡玩。密蘇里州首府傑斐遜城「
  7. 如果字符串不爲空,則使用window.confirm方法向用戶詢問:‘這是你最後的答案’
  8. 如果他們證實,寫類似於字符串?下面加入標籤:「密蘇里州的首府是」+ myanswer +「。所以你說。「
  9. 如果他們取消了,再次提問:」那麼密蘇里州的首都是什麼?「這一次,寫出答案沒有錯誤檢查。

回答

0

您的代碼的主要問題是您在JavaScript代碼中使用HTML代碼。

<script type="text/javascript"> -tag告訴瀏覽器:使用默認的JavaScript引擎執行下一個塊。但JavaScript引擎無法呈現或甚至無法理解HTML代碼。

開始創建一個簡單的模板:

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Homework No. 6</title> 
    </head> 
    <body> 


     <!-- place the script at the bottom to execute 
     AFTER the whole page has loaded. --> 
     <script type="text/javascript"> 
      // create the DIV Tag and insert it 
      // Answers: question 2 & 3 

      // THIS IS WHERE THE HTML-ELEMENT KICKS INTO THE PAGE 
      var div= document.createElement("div"); 
      div.innerHTML = "Lets play!"; 
      div.id = "questionContainer"; 

      // Insert the element into the body 
      document.body.appendChild(div); 


      var question = window.prompt("What is the capital of Missouri", ""); 

      // check if the question is empty 
      if (question.length < 1 || typeof question === "undefined") { 
       div.innerHTML = "Sorry you don't feel like playing :("; 
       return; 
      } 
      // check if user is sure (and repeat the question if he's not.) 
      if (!window.confirm("Are you sure?")) 
       question = window.prompt("What is the capital of Missouri", ""); 

      div.innerHTML = "The capital of Missouri is: " + question + " as you told me."; 
     </script> 
    <body> 
</html> 

這應該解決的問題。請記住:不要混合JavaScript和HTML。

另外:看看這個的jsfiddle行動來查看它:http://jsfiddle.net/du4CH/

+0

謝謝你的幫助..你們都認真幫助了我。我感到困惑,無法擺脫思路!那你直接指引我吧! – JBSAMOM 2013-03-31 00:58:08

0

這並不意味着寫一個新的div標籤,就意味着改變你已經寫好標籤,你所謂的「firstdiv」的內容之一。

+0

哇...我應該知道這是多麼愚蠢的錯誤。謝謝你指出。 – JBSAMOM 2013-03-31 00:56:49

0

6點實際上是在說「把它寫入div標籤」。假設它意味着您之前創建的div,則應該查看在文檔中定位div,然後寫入其innerHTML。像

if (moquestion.length < 1) { 
    document.getElementById("firstdiv").innerHTML="Sorry you don't feel like playing"; 
} 

應該做的伎倆。

+0

謝謝。你們都很棒! =)我覺得自己像個白癡 – JBSAMOM 2013-03-31 01:00:48