首页 > 编程语言 > 详细

javascript检索样式

时间:2015-09-14 11:52:51      阅读:207      评论:0      收藏:0      [点我收藏+]

今天看了《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:如有疑问,可以探讨。

javascript检索样式

原文:http://www.cnblogs.com/JustinBaby/p/4806524.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!