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:如有疑问,可以探讨。

  • 相关阅读:
    xamarin.Android ImageView 图片圆角(自定义属性、扩展控件)
    Protobuf简介
    创建高性能索引
    缓存表和汇总表
    Schema与数据类型优化
    香农理论在密码学中的应用
    传统密码技术
    自组织神经网络模型与学习算法
    径向基函数神经网络模型与学习算法
    BP神经网络模型与学习算法
  • 原文地址:https://www.cnblogs.com/JustinBaby/p/4806524.html
Copyright © 2011-2022 走看看