此函数用于处理子元素伪类的表达式:
var exprs = [ "3n+2" , "22", " + 1", " - 4 ", "-n", "3n", " odd ", " even " ] var parseNth = function(expr){ expr = expr.replace(/\s/g,''); var a = (expr === "even" && "2n0" || expr === "odd" && "2n1" || !/n/.test(expr) && ("0n"+expr) || expr.replace(/(^|\D+)n/g,"$11n") ).split(/n/); window.console && window.console.log([a[0]|0,a[1]|0]) } var i = 0, expr; while(expr = exprs[i++]){ parseNth(expr); }