Changing CSS classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pawe³

    #1

    Changing CSS classes

    Is there any way I can dynamically (taking advantage of JavaScript) change
    the properties for classes defined in CSS styles??
    Let say there's a CSS class ".regtex" and it forces the browser to display
    all elements of that class in red. Now I want to change this class so all
    elements belonging to this class are displayed in blue, however I don't
    want to change every single element, I just want to change the class'
    properties. Is it possible??
  • Richard Cornford

    #2
    Re: Changing CSS classes

    Pawe³ wrote:[color=blue]
    > Is there any way I can dynamically (taking advantage of
    > JavaScript) change the properties for classes defined in
    > CSS styles??[/color]

    On dynamic visual browsers supporting the - document.styleS heets -
    object (either in the Microsoft proprietary form or the W3C standard
    form), yes.
    [color=blue]
    > Let say there's a CSS class ".regtex"[/color]

    Class selectors introduce some potentially unexpected issues as the -
    selectorText - may be a normalised equivalent such as
    "*[class~=regtext]" rather than the more expected, original -
    selectorText - ".regtex", so some extra effort (indexOf or regular
    expression) may be necessary to identify the correct cssRule.
    [color=blue]
    > and it forces the browser to
    > display all elements of that class in red.[/color]

    Browsers cannot be forced to do anything, you just get to offer a
    suggestion of color that they may (but mostly will) follow.
    [color=blue]
    > Now I want to change this class so all elements
    > belonging to this class are displayed in blue,
    > however I don't want to change every single element, I
    > just want to change the class' properties.[/color]

    Assigning alternative values to the properties of the class's rule in
    the style sheet object in the stylesheets collection will alter the CSS
    acting on a page, in dynamic vi9sual browsers supporting the -
    document.styleS heets - collection. Other factors may override the actual
    presentation to the user.
    [color=blue]
    > Is it possible??[/color]

    Possible but not certain.

    Richard.


    Comment

    • Gus Richter

      #3
      Re: Changing CSS classes

      Pawel wrote:[color=blue]
      > Is there any way I can dynamically (taking advantage of JavaScript) change
      > the properties for classes defined in CSS styles??
      > Let say there's a CSS class ".regtex" and it forces the browser to display
      > all elements of that class in red. Now I want to change this class so all
      > elements belonging to this class are displayed in blue, however I don't
      > want to change every single element, I just want to change the class'
      > properties. Is it possible??[/color]

      One can access a collection of the style rules within a style sheet with
      "cssRules" in order to alter any of its properties. Say your stylesheet
      looked like this:

      <style type="text/css">
      a {color:green;}
      ..regtex {color:red;}
      </style>

      Then one can alter anchors to orange with:
      document.styles heets[0].cssRules[0].style.color. = "orange"
      And one can alter regtex classed elements to blue with:
      document.styles heets[0].cssRules[1].style.color. = "blue"

      You may also wish to look into "selectorTe xt" which is a string
      containing the value of the selector, where in your case, selectorText
      will contain ".regtex".

      Another way is to call another class which will override previous
      declarations. Here is an example which changes classes onmouseover:

      Disregard the fixed box which describes an Opera redraw bug.

      --
      Gus

      Comment

      Working...