首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在家工作帮助我被困在NUmber 6 Window.prompt()步骤

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

Stack Overflow用户
提问于 2013-03-27 12:35:58
回答 3查看 113关注 0票数 0

这是我的代码。我做错了什么。我在6号,它不工作了,能不能有人看看这个,给我一些帮助。谢谢

代码语言:javascript
复制
<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. 如果他们取消了,再问一个问题:“那么密苏里州的首都是什么?”这一次,在没有错误检查的情况下写出答案。
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-27 12:55:46

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

<script type="text/javascript">-tag告诉浏览器:使用默认JavaScript执行下一个块。但是JavaScript引擎不能呈现或者甚至不能理解HTML。

开始创建一个简单的模板:

代码语言:javascript
复制
<!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
EN

Stack Overflow用户

发布于 2013-03-27 12:48:22

它并不意味着编写一个新的div标记,它意味着更改您已经编写的标记的内容,您称之为"firstdiv“的标记。

票数 0
EN

Stack Overflow用户

发布于 2013-03-27 13:31:07

第6点实际上是说“将其写入div标记”。假设它意味着前面创建的div,您应该查看在文档上定位div,然后写入它的innerHTML。有点像

代码语言:javascript
复制
if (moquestion.length < 1) {
    document.getElementById("firstdiv").innerHTML="Sorry you don't feel like playing";
}

应该能起作用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15658933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档