让nth-child在IE8及更低版本中工作

时间:2012-12-21 12:25:29

标签: jquery css internet-explorer-8 css-selectors

我正在使用以下代码:

.c-1:first-child, .c-2:first-child, .c-1:nth-child(4n+1) { margin-left: 0; }

效果很好,但我需要模仿不支持nth-child的浏览器,比如IE8。

我已经尝试过这个jQuery代码来添加一个类,但没有任何反应,这个代码是对的吗?

// Support nth child in IE8
$('.c-1:first-child').addClass('remove');
$('.c-2:first-child').addClass('remove');
$('.c-1:nth-child(4n+1)').addClass('remove');

4 个答案:

答案 0 :(得分:4)

你可以使用jQuery的.eq(<index>)

例如:

$('.c-1').eq(0).addClass('remove');

答案 1 :(得分:1)

请查看以下文章:

http://abouthalf.com/2011/07/06/poor-mans-nth-child-selector-for-ie-7-and-8/

但是所描述的解决方案仅适用于ie7和8,在ie6中不会遗憾

答案 2 :(得分:0)

有一个库可以填充IE中缺少的CSS功能:

http://code.google.com/p/ie7-js/

使用IE9版本,您可以根据功能列表访问:nth-child()http://ie7-js.googlecode.com/svn/test/index.html

答案 3 :(得分:0)

我建议使用http://selectivizr.com/

对于伪元素IE8。 我知道它的旧帖子,但也许some1会发现它很有用:)