views:

47

answers:

1

Hi,

I have the following string which is a CSS selector:

#downloads > ul > li:nth-of-type(1) > ul > li:nth-of-type(3) > a

This CSS selector works fine in FireFox, CHrome and Safari but IE 6 does not support the nth-of-type selector. The CSS selectors I am working with are generated by Nokogiri and I can not change them.

Threw testing I have got the following working:

#downloads > ul > li:nth(0) > ul > li:nth(2) > a

Changing the nth selector to a plain nth and subtracting one from the nth value. I have been trying to programmitcally with JS to convert a CSS selector from using nth-of-type to normal nth? So after running it threw:

#downloads > ul > li:nth-of-type(1) > ul > li:nth-of-type(3) > a

Would become

#downloads > ul > li:nth(0) > ul > li:nth(2) > a

Cheers

Eef

+1  A: 

Javascript

var str = "#downloads > ul > li:nth-of-type(1) > ul > li:nth-of-type(3) > a";
str = str.replace(/:nth-of-type\(([0-9]+)\)/g, function(match, first) {
   return ":nth(" + (parseInt(first)-1) + ")";
});

alert(str); // -> #downloads > ul > li:nth(0) > ul > li:nth(2) > a

[See it in action]

galambalazs