WebReference.com logo
tip archive  •   about  •   sitemap  •   contact  •   jobs  •   write for us  •   subscribe


[previous] [next]

Working with the DOM Stylesheets Collection [con't]

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume

The deleteMedium() method deletes one media type at a time. Deleting a medium not in the list, or that the browser doesn't understand, will cause it to throw a 'Node not found' error. Deleting all media types from the list will cause the media text to be empty, so it will once again be applied to all media types:

You can preemptively avoid the 'Node not found' error by looping through the listed media types using the item() method and verifying that the media type is in the list before deleting it:

The following function would add a new media type to all the style sheets in a page. It returns the number of additions that were made:

Accessing the Stylesheet Directly

Another way to access the styleSheet object is through the tag that contains it. Once you obtain a reference to the LINK or STYLE element, you can reference the associated styleSheet object using the sheet property of the element:

The CSS Rule object

Presumably, obtaining a reference to the a styleSheet object is only the first step. The real goal is to manipulate the rules contained therein. The css rules are exposed via the cssRules property in DOM compliant browsers and the rules property in Internet Explorer.

The cssRules object contains five properties:

The following example retrieves the rule for <P> tags within the first style sheet in the page:

Stylesheet Methods

The methods to add and delete rules from a style sheet depend on the browser you are coding for. Your choices are:

Internet Explorer DOM Compliant Browsers
addRule(selector, declaration, [index]): adds a new rule to the style sheet, where the parameter selector is the rule's selector text (i.e.: "p," "div b" etc.), and "declaration" is the rule's properties and corresponding values(i.e.: background-color: yellow; color: brown). An optional index parameter (integer) lets you specify the position of the new rule within the style sheet, whereby 0, for example, would insert the new rule as the very first one (default is -1, which adds the new rule to the end of the style sheet). insertRule(rule, index): Inserts a new rule to the style sheet, where the parameter rule is a string containing the entire rule to add (e.g.: #myid{color: red; border: 1px solid black}), and index, an integer specifying the position within cssRules[] to insert the new rule.
removeRule([index]): removes the first rule of a style sheet. Enter an optional index (integer) to remove a specific rule based on its position in the rules[] collection. deleteRule(index): Removes a rule from the style sheet based on its position in the cssRules[] collection. Use the parameter index (integer) to specify this position.


[previous] [next]

Recent Articles

WebReference.com site name
Building a Banking Application Home Page with OOP
Mixing Scripting Languages
Review: phpFox, a Social Networking CMS with all the Bells and Whistles
internet.com site name
Enterprise 2.0: Social Networking in the Cloud
BroadSoft Marketplace Hastens Pace of Telephony Innovation
Review: HTC Hero for Sprint


internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs