2013-03-27 42 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