为什么第一个警告框是空的,而第二个警告框却显示 100px? 我希望第一个框显示 300px..
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Move</title>
<style type="text/css">
div#b-ball {
position:absolute;
top:300px;
left:300px;
}
</style>
<script type="text/javascript">
<!--
function moveBall() {
alert(document.getElementById("b-ball").style.top);
document.getElementById("b-ball").style.top="100px";
alert(document.getElementById("b-ball").style.top);
}
//-->
</script>
</head>
<body onload="moveBall()">
<div id="b-ball">
Basketball image here.
</div>
</body>
</html>
最佳答案
样式集合只包含直接应用于元素的样式,不包含从样式表继承的样式。
关于javascript - 使用 getElementById 检索 CSS 中的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1539753/