首页 > 其他 > 详细

ie9中的classList实现

时间:2017-11-07 13:53:01      阅读:446      评论:0      收藏:0      [点我收藏+]

(function(){
  if(document.body.classList == null && Element){
    var wjClassList = {
      el: null,
      names: [],
      getClass: function(){
        var cNames = this.el.className;
        this.names = cNames ? cNames.trim().split(/\s+/) : [];
      },
      genClass: function(){
        this.el.className = this.names.join(" ");
      },
      add:function(cName){
        var i = this.contains(cName);
        if(i === false){
          this.names.push(cName);
          this.genClass();
        }
      },
      remove: function(cName){
        var i = this.contains(cName);
        if(typeof i == "number"){
          this.names[i] = "";
          this.genClass();
        }
      },
      toggle: function(cName){
        var i = this.contains(cName);
        if(i === false){
          this.add(cName);
        }else{
          this.remove(cName);
        }
      },
      contains: function(cName){
        this.getClass();

        var i, len = this.names.length;
        for(i = 0; i < len; i++){
          if(this.names[i] == cName){
            return i; // 如果存在,返回索引
          }
        }
        return false;
      },
    };

    // 在不支持classList的浏览器中, 在Element的原型中写入此方法
    Object.defineProperty(Element.prototype, ‘classList‘, {
      get: function(){
        wjClassList.el = this;
        return wjClassList;
      }
    });
  }
})();

ie9中的classList实现

原文:http://www.cnblogs.com/wuzhike/p/7798644.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!