家庭工作帮助我被困在NUmber 6 Window.prompt()步骤

时间:2013-03-27 12:35:59

标签: javascript html

这是我的编码..我做错了什么。我在第6号并且它不起作用有人可以看看这个并给我一些帮助。 感谢

<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. 如果他们取消了,再问一次问题:“那么密苏里州的首都是什么?”这一次,写下答案而不进行错误检查。

3 个答案:

答案 0 :(得分:0)

这并不意味着要写一个新的div标签,它意味着要更改您已经写过的标签的内容,即您所谓的“firstdiv”。

答案 1 :(得分:0)

您的代码的主要问题是您在JavaScript代码中使用HTML代码。

<script type="text/javascript"> - 标记告诉您的浏览器:使用默认的JavaScript-Engine执行下一个块。但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/

答案 2 :(得分:0)

第6点实际上是在“将其写入div标签”。假设它表示您之前创建的div,您应该查看在文档上定位div然后写入其innerHTML。像

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

应该这样做。