views:

74

answers:

2

Or do I have to do something like this:

var nodes = document.childNodes;
for (var i in nodes) {
    if (window.getComputedStyle(nodes[i], null).getPropertyValue('someproperty') == 'somevalue')
        // do stuff
}

Edit:

I'm not very familiar with XPath. A 'simple' stab at the problem would be something like this:

function test() {
    var resultSet = document.evaluate("//*[@float='left']", document.body, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    for (var i = 0; i < resultSet.snapshotLength; i++) {
        var element = resultSet.snapshotItem(i);
        alert(element);
    }
}

But unsurprisingly this doesn't work, since float is a property, not an attribute...

+1  A: 

http://blog.stchur.com/2006/06/21/css-computed-style/

knut
I believe this selects for attributes, not style properties. Also, I'd prefer a native Javascript solution, and Javascript doesn't have CSS selector support built-in.
int3
I misunderstood your question. Sorry about that. You may use the computed style.
knut
oops. Actually my first code snippet was meant to use getComputedStyle, but I left that function call out. Yep aside from that your link is functionally equivalent to what I originally posted... I guess there's really no better way.
int3
I think so too.
knut
+1  A: 

As Viet & knut said before, you can go ahead with attribute selectors & string matching functions: http://www.w3schools.com/xpath/xpath_functions.asp#string.

You should not confuse XPath with Javascript :)

I have hint for you. Say you have a node:

<a href="http://google.com" style="padding: 10px; float: left; margin: 10px auto;">Look at me!</a>

Use fn:substring-after("padding: 10px; float: left;", "float:") to get the " left; margin: 10px auto;".

And then use fn:substring-before(" left; margin: 10px auto;", ";") to get the " left".

After this, use fn:normalize-space(" left") to get "left" :)

Cyberman
I think the Javascript method is more elegant compared to this. Nonetheless, thanks for answering the question!
int3
Yes indeed, but this was what you were asking for. I only use XPath when I don't have JS engine.
Viet