javascript设置样式属性覆盖

时间:2018-05-21 07:54:21

标签: javascript properties set prototype

(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 个答案:

没有答案