下面是HTML:爲什麼我的Javascript錯誤地與這個元素交互?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
MYNAME
11/3/2010
CISC 131
Die.html uses the html within it and the style sheet located in Die.css to create a die whoes functionality is created in Die.js
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="Die.css" rel="stylesheet" type="text/css"/>
<script src="Die.js" type="text/javascript"></script>
<title>Die</title>
</head>
<body>
<div id="game">
<div id="clickableArea" class="banner">
</div>
<div id="dice">
<div id="dot" class="die">
<div id="dot0" class="dot rowOne colOne">•</div><div id="dot1" class="dot rowOne colTwo">•</div><div id="dot2" class="dot rowOne colThree unused">•</div>
<div id="dot3" class="dot rowTwo colOne">•</div><div id="dot4" class="dot rowTwo colTwo">•</div><div id="dot5" class="dot rowTwo colThree">•</div>
<div id="dot6" class="dot rowThree colOne unused">•</div><div id="dot7" class="dot rowThree colTwo">•</div><div id="dot8" class="dot rowThree colThree">•</div>
</div>
<div id="dotB" class="die">
<div id="dotB0" class="dot rowOne colOne">•</div><div id="dotB1" class="dot rowOne colTwo">•</div><div id="dotB2" class="dot rowOne colThree unused">•</div>
<div id="dotB3" class="dot rowTwo colOne">•</div><div id="dotB4" class="dot rowTwo colTwo">•</div><div id="dotB5" class="dot rowTwo colThree">•</div>
<div id="dotB6" class="dot rowThree colOne unused">•</div><div id="dotB7" class="dot rowThree colTwo">•</div><div id="dotB8" class="dot rowThree colThree">•</div>
</div>
</div>
<div id="status" class="banner">
</div>
</div>
</body>
</html>
然後這裏是CSS:
/*
MYNAME
11/3/2010
CISC 131
Dice.css helps Die.html create a Die to be maniupulated by Die.js
*/
*
{
margin: 0;
padding: 0;
}
body
{
font-family: "Times New Roman";
font-size: 12pt;
background-color: #0033CC;
}
.die
{
width: 6em;
height: 6em;
border-style: solid;
border-color: black;
border-width: .25em;
position: relative;
float: left;
margin: 1em;
background-color: #FFFFFF;
}
.dot
{
font-size: 5em;
line-height: .30em;
float: left;
position: relative;
color: red;
}
.banner
{
height: 1.5em;
background-color:#000000;
color: #FFFF00;
}
.rowOne
{
top: .05em;
}
.rowTwo
{
top: .15em;
}
.rowThree
{
top:.25em;
}
.colOne
{
left: .03em;
}
.colTwo
{
left: .08em;
}
.colThree
{
left: .13em;
}
.unused
{
visibility: hidden;
}
#game
{
margin: auto;
width: 20em;
height: 15em;
background-color: #006600;
position: relative;
top: 4em;
}
#dice
{
margin-left: 1.5em;
width: 20em;
height: 8em;
}
#status
{
position: relative;
bottom: -5em;
}
這裏的問題是代碼:
messageLocation=document.getElementById("status");
...
window.alert("alert");
messageLocation.innerHTML="test";
window.alert("alert");
messageLocation.innerHTML="posttest";
這是發生了什麼事S:
- 一個警告框彈出,上面寫着警告
- 字測試是寫在左側的messageLocation元素
- 另一個警告框彈出
- 詞「後測」被寫入messageLocation元素的最右側。
messageLocation
是一個由其ID標識的div元素。
爲什麼4會發生?爲什麼不像測試那樣將測驗寫在左側?
什麼樣的元素是messageLocation? – wombleton 2010-11-08 04:12:10
eddited回答。 – David 2010-11-08 04:14:38
這將有助於看到HTML/CSS來解決這個問題。 – dpmguise 2010-11-08 04:24:22