Get/Set CSS property values via [removed] questions

前端 未结 3 1210
感动是毒
感动是毒 2020-12-06 08:11

Some things are unclear to me:

var myDiv = document.getElementById(\"myDiv\");
var computedStyle = window.getComputedStyle(myDiv);

1) Isn\'

3条回答
  •  既然无缘
    2020-12-06 08:41

    Isn't it possible to directly get global border color of a div if there is only one color, the same for each side

    Yes and No. The spec describes two methods:

    • getPropertyCSSValue() returns a CSSValue of a single CSS Property. It does not work with shorthand properties.
    • getPropertyValue() returns a DOMString, and works also for shorthand properties. But you need to be careful when there are different borders, the string will represent all of them.

    When having style properties in a CSS file, they are only accessible through the getComputedStyle method

    No. They are also accessible through document.styleSheets (spec), and can be changed with the StyleSheet interface.

    ...and not via the style property like style properties defined inline, via a style attribute in the div, I'm right?

    Yes. The .style property represents only the style declaration in the style attribute (inline styles).

    If we want to set a style property, we have to use the style attribute of the element

    I guess you mean a CSS property. You can also influence the computed style by setting classes on your element (or anything else that applies other styles through a stylesheet). Or you can create stylesheets dynamically, and they will be applied on the document. You can also set the style attribute of an element, but usually you will use the CSSStyleDeclaration interface exposed by the .style property.

    is it not possible using the computed style object?

    Yes. The spec says that the returned "CSSStyleDeclaration is read-only and contains only absolute values".

提交回复
热议问题