这是我的编码..我做错了什么。我在第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>
标记中写入如下内容:“抱歉,您不想玩。密苏里州的首府是杰斐逊城。“答案 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";
}
应该这样做。