这是我的代码。我做错了什么。我在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>以下是作业
<body>中,创建一个带有元素id的<div>标记。<div>标记中插入一些文本。<div>下面,以便在加载完该脚本之前不会尝试运行。<div>标记中写入如下内容:“对不起,您不想玩。密苏里的首都是杰斐逊城。“发布于 2013-03-27 12:55:46
代码的主要问题是在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/
发布于 2013-03-27 12:48:22
它并不意味着编写一个新的div标记,它意味着更改您已经编写的标记的内容,您称之为"firstdiv“的标记。
发布于 2013-03-27 13:31:07
第6点实际上是说“将其写入div标记”。假设它意味着前面创建的div,您应该查看在文档上定位div,然后写入它的innerHTML。有点像
if (moquestion.length < 1) {
document.getElementById("firstdiv").innerHTML="Sorry you don't feel like playing";
}应该能起作用。
https://stackoverflow.com/questions/15658933
复制相似问题