zoukankan      html  css  js  c++  java
  • web标准中定义id与class有什么区别吗

     
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh" lang="zh">
        <head profile="http://www.w3.org/2000/08/w3c-synd/#">
        <meta http-equiv="content-language" content="zh-cn" />
        <meta http-equiv="content-type" content="text/html;charset=gb2312" />
        <title>blueidea</title>
        <style type="text/css">
        /*<![CDATA[*/
        #aa {
        color:red
        }
        .aa {
        color:blue
        }
        /*]]>*/
        </style>
        </head>
        <body>
        <div id="aa" class="aa">
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
        </div>
        </body>
        </html>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh" lang="zh">
    <head profile="http://www.w3.org/2000/08/w3c-synd/#">
    <meta http-equiv="content-language" content="zh-cn" />
    <meta http-equiv="content-type" content="text/html;charset=gb2312" />
    <title>blueidea</title>
    <style type="text/css">
    /*<![CDATA[*/
    .a {
    color:orange
    }
    .b {
    background:#eee
    }
    .c {
    border:1px solid red
    }
    /*]]>*/
    </style>
    </head>
    <body>
    <div class="a b c">aaaaaaaa</div>
    </body>
    </html>

    一.

    web标准中是不容许重复ID的,比如 div id="aa" 一个页面中不容许重复2次,而class 定义的是类,理论上可以无限重复的,这样需要多次引用的定义便可以使用他.

    class还可以同时引用多个类,不同的类之间用空格隔开.


    二.

    属性的优先级问题,ID 的优先级要高于class,看上面的例子


    三.

    方便JS等客户端脚本,如果在页面中要对某个对象进行脚本操作,那么可以给他定义一个ID,否则只能利用遍历页面元素加上指定特定属性来找到它,这是相对浪费时间资源,远远不如一个ID来得简单.

  • 相关阅读:
    Python 通过最低位判断数字是偶数还是奇数
    C语言 windows下Ansi和UTF-8编码格式的转换
    C语言 Ubuntu系统 UTF-8 文字处理
    C语言 使用char字符实现汉字处理
    C# GUI应用 实现 2048游戏
    Docker部署Minio
    Ubuntu18.04开启root
    Ubuntu18.04安装Docker
    docker安装mysql
    idea启动项目端口被占用
  • 原文地址:https://www.cnblogs.com/aobingyan/p/3762859.html
Copyright © 2011-2022 走看看