家庭作业帮助我被困在第6个窗口提示()步骤

Home work help I am stuck at NUmber 6 Window.prompt() step

本文关键字:提示 窗口 步骤 6个 帮助 家庭作业      更新时间:2023-09-26

这是我的编码。我做错了什么?我在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. 使用窗口。快速提问"密苏里州的首府是什么?"
  6. 检查以确保返回字符串的长度属性不小于1。如果它是空的,在<div>标签中写如下内容:"对不起,你不想玩。密苏里州的首府是杰斐逊城。"
  7. 如果字符串不为空,则使用该窗口。确认方法问用户:"这是你的最终答案吗?"
  8. 如果他们确认,写一个类似下面的字符串到标签:"密苏里的首都是" + myanswer + "。你也这么说。"
  9. 如果他们取消了,再问一次问题:"那么密苏里州的首都是哪里?"这一次,写出答案,不做错误检查。

你的代码的主要问题是你在JavaScript代码中使用html代码。

<script type="text/javascript"> -标签告诉你的浏览器:使用默认的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/

这并不意味着写一个新的div标签,它意味着改变你已经写的标签的内容,你称之为"firstdiv"。

第6点实际上是说"将其写入div标签"。假设它指的是您之前创建的div,您应该查找文档上的div,然后写入其innerHTML。就像

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