Get border value with getComputedStyle().getPropertyValue()? (Mozilla, FF)

前端 未结 1 1737
耶瑟儿~
耶瑟儿~ 2021-01-21 16:30

In some browsers (namely, Firefox) the getComputedStyle().getPropertyValue() doesn\'t report anything for shorthand CSS, like border. Is there a non-sp

1条回答
  •  借酒劲吻你
    2021-01-21 16:50

    I'm wondering, what do you want to do with a string like border: 1px solid #000?

    Say you want to reproduce an elems border in order to copy it copyStyle(el2, el, "border"):

    // Copies a set of styles from one element to another.
    function copyStyle(dest, source, shorthand) {
      var computed = window.getComputedStyle(source, null);
      for (var i = computed.length; i--;) {
        var property = camelize(computed[i]);
        if (property.indexOf(shorthand) > -1) {
          console.log(property)
          dest.style[property] = computed[property];
        }
      }
    }
    
    // prototype.js
    function camelize(text) {
      return text.replace(/-+(.)?/g, function (match, chr) {
        return chr ? chr.toUpperCase() : '';
      });
    }
    

    Comparing if two element's given set of styles matches can be done in the same manner. Other than that, I really can't see the use a string, which should be parsed if you want to compute anything with it.

    0 讨论(0)
提交回复
热议问题