Changing CSS Values with Javascript

后端 未结 9 1516
借酒劲吻你
借酒劲吻你 2020-11-22 17:16

It\'s easy to set inline CSS values with javascript. If I want to change the width and I have html like this:

<
9条回答
  •  旧时难觅i
    2020-11-22 17:44

    You can get the "computed" styles of any element.

    IE uses something called "currentStyle", Firefox (and I assume other "standard compliant" browsers) uses "defaultView.getComputedStyle".

    You'll need to write a cross browser function to do this, or use a good Javascript framework like prototype or jQuery (search for "getStyle" in the prototype javascript file, and "curCss" in the jquery javascript file).

    That said if you need the height or width you should probably use element.offsetHeight and element.offsetWidth.

    The value returned is Null, so if I have Javascript that needs to know the width of something to do some logic (I increase the width by 1%, not to a specific value)

    Mind, if you add an inline style to the element in question, it can act as the "default" value and will be readable by Javascript on page load, since it is the element's inline style property:

    ....

提交回复
热议问题