If you're looking to check for support of the :nth-child pseudo-class, I've got a function that can help with that
function checkNthChildSupport(){
var result = false,
test = document.createElement('ul'),
style = document.createElement('style');
test.setAttribute('id', 'nth-child-test');
style.setAttribute('type', 'text/css');
style.setAttribute('rel', 'stylesheet');
style.setAttribute('id', 'nth-child-test-style');
style.innerHTML = "#nth-child-test li:nth-child(even){height:10px;}";
for(var i=0; i<3; i++){
test.appendChild(document.createElement('li'));
}
document.body.appendChild(test);
document.head.appendChild(style);
if(document.getElementById('nth-child-test').getElementsByTagName('li')[1].offsetHeight == 10) {result = true;}
document.body.removeChild(document.getElementById('nth-child-test'));
document.head.removeChild(document.getElementById('nth-child-test-style'));
return result;
}
To use this function:
checkNthChildSupport() ? console.log('Yes, :nth-child is supported') : console.log('No, :nth-child is NOT supported');
You can test it out live here:
http://jsbin.com/epuxus/15
It's worth noting the distinction between jQuery's :nth-child
and CSS's :nth-child
.
While jQuery's :nth-child
is compatible with any browser that supports jQuery, CSS's :nth-child
has limited support in IE9, Chrome, Safari, and Firefox.