我有以下字符串,它是一个CSS选择器:
#downloads > ul > li:nth-of-type(1) > ul > li:nth-of-type(3) > a
此CSS选择器在FireFox、CHrome和Safari中运行良好,但IE6不支持第n个类型的选择器。我正在使用的CSS选择器是由Nokogiri生成的,我无法更改它们。
抛出测试我已经完成了以下工作:
#downloads > ul > li:nth(0) > ul > li:nth(2) > a
将第n个选择器更改为普通的第n个,并从第n个值减去1。我一直在尝试用JS编程将CSS选择器从使用nth-of-type转换为正常的nth?所以在运行它之后抛出:
#downloads > ul > li:nth-of-type(1) > ul > li:nth-of-type(3) > a
会变成
#downloads > ul > li:nth(0) > ul > li:nth(2) > a
干杯
Eef
发布于 2010-07-07 23:40:57
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]
https://stackoverflow.com/questions/3196193
复制相似问题