2

I know that Attr gets the exact class of an element. Is there a way to get the element exact class as defined in CSS? For example an element may have class "title" but be defined in CSS as a descendant selector, something like: .other-list .title {} I need something to get that exact class value. You can see here the full example: http://jsfiddle.net/2TMgQ/2/

On click both elements have class "title" but are defined in CSS as 2 different elements. Thank you

Mircea
  • 11,373
  • 26
  • 64
  • 95
  • You *can* do this, but do you **need** to? It's possible, Firebug Lite does this, but it isn't pretty: http://fbug.googlecode.com/svn/lite/branches/firebug1.4/content/firebug/css.js – Nick Craver Jul 06 '10 at 20:59
  • YEap, I need to. Indeed Firebug does it. I do not think that i will be able to understand the FireBug code. That was the first place i had looked in to, I thought that there is an easier way... how about this: https://developer.mozilla.org/en/DOM/cssRule.selectorText – Mircea Jul 06 '10 at 21:06
  • Indeed they are using "selectorText", just looked again to their code. Can't figure it out how, the code is to big to understand it – Mircea Jul 06 '10 at 21:12
  • 2
    @Mircea - Let me put it this way...the Firebug guys are good, and they wouldn't do it a way that's more complicated that was required (*for them*). You can strip from their implementation because you don't need *all* that functionality, but just finding the rules isn't easy. For example what if 10 rules applied to your element, then what? Which do you use? It could always be a list, what if your example was this instead? http://jsfiddle.net/2TMgQ/9/ – Nick Craver Jul 06 '10 at 21:18
  • @Nick, I an sure that FireBug guys are top. In their code is much functionality I do not need. I nyour example I would take .links-list .title and .other-list .title for second – Mircea Jul 06 '10 at 21:20
  • @Mircea - Right...but how would the *code* know which ones you wanted? Why those and not the others? I ask because you'll have to turn whatever reasoning you have into code/logic that always works. – Nick Craver Jul 06 '10 at 21:22
  • Hmmm, good question. I do believe that I need the most specific selector from an element THAT is also defined in the external CSS. Hard thing to do, I was hoping that jQuery have something for this – Mircea Jul 06 '10 at 21:35
  • I once tried to get jQuery to do the dishes $("plates","glasses").wash(); – Marko Jul 06 '10 at 21:39
  • 1
    @Marko - Why were the plates *in* the glasses? Was it really big glasses or really little plates? – Nick Craver Jul 06 '10 at 21:42
  • Crap so that's why it didn't work, I'm gonna try $("plates,glasses").wash(); My mother will be proud! – Marko Jul 06 '10 at 21:43

3 Answers3

1

This should get you all CSS styles that apply to the element. It uses jQuery's is function to check if a CSS rule selector applies to a given element. Iterate through all stylesheets in document.styleSheets, and through all cssRules in each stylesheet, and check if selectorText matches the element. In pseudocode:

given::element

matchedRules = []

for styleSheet in document.styleSheets
    for rule in styleSheet.cssRules
        if element.is(rule.selectorText)
            matchedRules.push(rule.selectorText)

return matchedRules;

In code:

function getCSSRules(element) {
    element = $(element);

    var styleSheets = document.styleSheets;
    var matchedRules = [], rules, rule;

    for(var i = 0; i < styleSheets.length; i++) {
        rules = styleSheets[i].cssRules;

        for(var j = 0; j < rules.length; j++) {
            rule = rules[j];
            if(element.is(rule.selectorText)) {
               matchedRules.push(rule.selectorText);
            }
        }
    }

    return matchedRules;
}

This should return all rules that apply to the element. For your first example, it returns:

[".links-list .title"]

For Nick's extended example, it returns:

[".links-list .title", ".title", "a", "li a"]

How you define specificity and how to decide what to filter out from this list is upto you.

Anurag
  • 140,337
  • 36
  • 221
  • 257
0

If you are referring to parent - child selectors then the syntax would be something like:

$(".other-list > .title")

Have a look at http://api.jquery.com/category/selectors/

gnome
  • 1,113
  • 2
  • 11
  • 19
0

Maybe this could help

CSS parser/abstracter? How to convert stylesheet into object

Community
  • 1
  • 1
Philippe
  • 1,733
  • 1
  • 14
  • 28