Finding Inactive CSS Properties

Is it possible to define styles in a CSS file via Javascript?

I am trying to detect CSS properties that will be applied to an element in a specific state, :hover

in this case, but without those properties currently active on the element. I thought about cloning the element, adding the clone as sibling with display: none

and requesting the properties that way, but I don't know how to get the style to :hover

use Javascript. Any ideas?

0


source to share


4 answers


I believe you are interested in an array styleSheets

, which is a property of document

( document.styleSheets

). This array indexes all of the style sheets referenced by the current page and allows you to iterate over all the style rules on each sheet. The W3C (Firefox, Opera, Safari) array for accessing CSS rules is cssRules

, whereas the Microsoft rules

. The following code snippet is taken from www.quirksmode.org :


var theRules = new Array();
if (document.styleSheets[1].cssRules)
    theRules = document.styleSheets1.cssRules
else if (document.styleSheets[1].rules)
    theRules = document.styleSheets1.rules

      





Now theRules

contains the rules for the second style sheet for the document. See the Quirksmode link and compatibility tables for more information on accessing CSS rules from JavaScript.

+3


source


the hover style applied by the browser depends on mouse movement. I don't think you can force it through JS. But why do you need it? just define another css class and assign it via className property in JS.



0


source


To define styles in JS, you can do it with className, for example:

item.onmouseover = gohover;

item.onmouseout = nohover;

gohover () function {this.className = gClass;}

...

0


source


If I understand your question correctly, it seems that you are looking for XPath for CSS (pseudo) classes. Apart from collecting a CSS file using Ajax and searching with a regular expression, I don't know of any method to extract style information from a CSS file.

0


source







All Articles