zoukankan      html  css  js  c++  java
  • 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:如有疑问,可以探讨。

  • 相关阅读:
    mysql中的几种join 及 full join问题
    MySQL基础练习题
    SQL之IFNULL()
    SQL之查找表中字段的值相同的记录
    Mysql之将一张表内容导入另一张表中
    selenium无界面操作浏览器与Chrome Options的启动项设置
    SQL UNION 和 UNION ALL 操作符
    Python断言方法assert
    Python标准库--contextlib模块
    Python标准库--itertools模块
  • 原文地址:https://www.cnblogs.com/JustinBaby/p/4806524.html
Copyright © 2011-2022 走看看