javascript检索样式
今天看了《javascript DOM 编程艺术》第二版的第150页:书上说javascript只能获取内联样式的值,无法获取内部样式表和外部样式表的值,特作此测试:
内联样式表:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p id="example" style="color:grey; font: 12px 'Arial', sans-serif">what fuck</p>
<script type="text/javascript">
var para = document.getElementById("example");
alert(para.style.fontSize);
</script>
</body>
</html>
内联样式表:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
#example{
color:grey;
font: 12px 'Arial', sans-serif;
}
</style>
</head>
<body>
<p id="example">what fuck</p>
<script type="text/javascript">
var para = document.getElementById("example");
alert(para.style.fontSize);
</script>
</body>
</html>
外部样式表:
html部分的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<p id="example">what fuck</p>
<script type="text/javascript">
var para = document.getElementById("example");
alert(para.style.fontSize);
</script>
</body>
</html>
index.css部分的代码:
#example{
color:grey;
font: 12px 'Arial', sans-serif;
}
经调试:
chrome firefox opera safari IE都遵循javascript只能检索内联样式表的值。
ps:如有疑问,可以探讨。