zoukankan      html  css  js  c++  java
  • js中设置元素class的三种方法小结

     一、el.setAttribute('class','abc');
    代码如下:

     .abc {
        background: red;
      }
    test div
     var div = document.getElementById('d1');
     div.setAttribute("class", "abc");
        IE6/7 : div背景色不是红色
        IE8/9/10/Firefox/Safari/Chrome/Opera : div背景色为红色
        结果:IE6/7不支持setAttribute('class',xxx)方式设置元素的class。
     二、el.setAttribute('className', 'abc')
       
    代码如下:
     
      .abc {
        background: red;
        }
    test div

        var div = document.getElementById('d1');
        div.setAttribute("className", "abc");

        IE6/7 : div背景色为红色
        IE8/9/10/Firefox/Safari/Chrome/Opera : div背景色不是红色
        结果:IE8/9/10/Firefox/Safari/Chrome/Opera不支持setAttribute('className',xxx)方式设置元素的class。
        很有趣,使用setAttribute的时候第一个参数为class和className的情形在IE6/7和IE8/9/10/Firefox/Safari/Chrome/Opera刚好相反。
    三、el.className = 'abc';
      代码如下:
     .abc {
      background: red;
      }
    test div
       var div = document.getElementById('d1');
       div.className = 'abc';
        所有浏览器都支持。

    原文地址:http://www.shangxueba.com/jingyan/1906397.html




  • 相关阅读:
    hdu1698(线段树区间更新)
    js数组的操作
    grunt构建一个项目
    JS获取当前时间
    页面打开后,几秒后自动跳转
    设置网页图片热点链接
    mongodb的安装
    Linux,activemq-cpp之消息过滤器
    Linux 命令行输入
    第五篇——Spring音乐播放界面设计(C#)
  • 原文地址:https://www.cnblogs.com/liuyandeng/p/5824023.html
Copyright © 2011-2022 走看看