zoukankan      html  css  js  c++  java
  • Javascript兼容性之——getAttribute(),setAttribute()(获取设置属性)

    做前端的,总是要跟兼容性打交道,CSS兼容性,JS兼容性,这里我总结了一些getAttribute(),setAttribute()在不同浏览器下兼容性以及如何解决这些问题:

     1     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
     2     <html xmlns="http://www.w3.org/1999/xhtml">  
     3     <head>  
     4     <title>kingwell</title>  
     5     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
     6     <body>  
     7       
     8     <div id="idHeader" class="class-header" title="kingwell" status="1"></div>  
     9     <label id="forUserName" for="userName" title="kingwell" status="1"></label>  
    10       
    11     </body>  
    12     </html>  
     1     var el = document.getElementById("idHeader");  
     2     alert(el.getAttribute("id"));  
     3     alert(el.id);  
     4     IE Firfox->idHeader  
     5       
     6     alert(el.getAttribute("class"));  
     7     //IE6,IE7 -> null IE8,IE9,Firefox ->class-header  
     8       
     9     alert(el.class);  
    10     //IE6,IE7,IE8->报错 IE9,Firefox->undefined  
    11     alert(el.getAttribute("className"));  
    12     //IE6,IE7->class-header ; IE8,IE9,Firefox -> undefined  
    13       
    14     alert(el.className);  
    15     //All -> class-header  
    16       
    17       
    18     var elfor = document.getElementById("forUserName");  
    19     alert(elfor.getAttribute("for"));  
    20     //IE6,IE7->undefined IE8,9,Firefox->forUseName  
    21       
    22     alert(elfor.for )  
    23     //IE6,IE7报错,其它为undefined  
    24     alert(elfor.title)  
    25     //全部输出kingwell  
    26     alert(elfor.status);  
    27     //IE6-8 -> 1 IE9,Firefox->undefined  
    28       
    29     alert(elfor.getAttribute("status"))  
    30     //全部输出 1   

        /*总结:
        1:常规属性建议使用 node.XXXX。
        2:自定义属性建议使用node.getAttribute("XXXX")。
        3:当获取的目标是JS里的关键字时建议使用node.getAttribute("XXX"),如label中的for。
        4:当获取的目标是保留字,如:class,请使用className代替。
        */

    这里是分析的是getAttribute(),setAttribute()同理。

  • 相关阅读:
    数据库的架构和优化
    描述一个高性能高可靠的网站架构——如何设计一个秒杀系统
    PHP手册-函数参考-加密扩展
    系统性能指标总结
    PHP实现负载均衡的加权轮询
    PHP生成二维码
    高性能网站架构
    PHP实现Redis的数据结构和LFU/LRU
    缓存的设计及PHP实现LFU
    网络开发库从libuv说到epoll
  • 原文地址:https://www.cnblogs.com/kingwell/p/2673722.html
Copyright © 2011-2022 走看看