I want to get an element in the DOM, and then see what rules in my CSS files contribute to its appearance. Similar to what a firebug inspector or webkits does. Is there any way to do this in JavaScript?
Update:
I have to provide a restriction and a concrete example. I'm only interested in this in webkit-based browsers, so problems with multiple browsers are not so much a problem. What I'm specifically trying to achieve is this. Let's say I have a stylesheet as follows:
div { background: #f0f0f0; } .example { padding: 10px; }
And then let's say some kind of html code looks like this:
<div id="test" class="example"> <hgroup> <h1>Test</h1> <h2>A sample file to play with.</h2> </hgroup> <ul class="sample"> <li id="item_1">Item 1</li> <li id="item_2">Item 2</li> </ul> </div>
So then in javascript I want to have a function that can find selectors from CSS that style the object:
get_selectors_for(document.getElementById('test')) // should return: // ['div','.example']
How difficult is it to reset the query selector, knowing that we only need to worry about webkit and not all browsers?
javascript dom css
Jim Jeffers Dec 19 '10 at 9:47 2010-12-19 09:47
source share