We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

noah on 07/20/07


Tagged

css cool DOM safari theme flanagan broken stylesheets learning


Versions (?)


Who likes this?

3 people have marked this snippet as a favorite

joseluis
rhlowe
jatkins


Create new CSS Selector (FF, IE, Opera)


Published in: JavaScript 


Construct and add a selector to an existing stylesheet. Does not work in Safari.

  1. //see Flanagan 5th ed. 16.6
  2. var menuId = 'foo';
  3. var selector = '#'+ menuId + ' li ul';
  4. //assume we have at least one stylesheet
  5. if ( document.styleSheets[0].insertRule) {
  6. //if not IE
  7. var lastrule = document.styleSheets[0].cssRules.length;
  8. document.styleSheets[0].insertRule(selector + '{display:none}', lastrule);
  9. } else if ( document.styleSheets[0].addRule ) {
  10. //if IE
  11. var lastrule = document.styleSheets[0].rules.length;
  12. document.styleSheets[0].addRule(selector, 'display:none', lastrule);
  13. }

Report this snippet 

You need to login to post a comment.