javascript - 使用 getElementById 检索 CSS 中的样式

标签 javascript css dom html

为什么第一个警告框是空的,而第二个警告框却显示 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/

相关文章:

javascript - JS : Create a method to return an array that does not include the index values from the array passed to my method

javascript - 如何为我的日期选择器设置最小和最大日期?

c# - 值 'XX/YY/ZZZZ"对 DateTime 变量无效

javascript - 滚动时导航栏发生变化,无法使用库

javascript - innerhtml 字符串到多个元素

javascript - 使用javascript在html上显示和隐藏文本

php - 如何使一个 div 看起来部分覆盖在另一个方形 div 之上

html - 第 n 个 child (奇数)选择一切

javascript - 对 Sizzle 对象执行 DOM 方法时出错

javascript - 包装内容元素会触发内联脚本