代码之家  ›  专栏  ›  技术社区  ›  iMatoria

javascript集合样式属性覆盖

  •  1
  • iMatoria  · 技术社区  · 8 年前
    (function (constructor) {
      if (constructor && constructor.prototype) {
    
        //Custom - styleMe
        Object.defineProperty(constructor.prototype, 'styleMe', {
          get: function () {
            if (this == null) return {};
            return (window.getComputedStyle ? getComputedStyle(this, null) : this.currentStyle);
          },
          set: function (key, newValue) {
            this.styleMe.key = newValue;
          }
        });
      }
    })(window.Node || window.Element);
    

    get 风格价值如下:

    var marginTop = document.getElementById("myID").styleMe.marginTop;
    

    但是,如何设定价值?这不起作用:

    document.getElementById("myID").styleMe.marginTop = "30px";
    
    0 回复  |  直到 8 年前
    推荐文章