简体   繁体   English

IE 8中的第n个子选择器(伪类)

[英]nth-child selectors (pseudo classes) in IE 8

I need the expression 我需要表达

`tr:nth-child(2n){background-color: #ddd;}` 

to color each second row for a table which I create dynamically. 为我动态创建的表格的第二行着色。 But this expression works only for IE9. 但是此表达式仅适用于IE9。 So is it possible to enable these CSS3 pseudo classes for IE8, too? 那么是否也可以为IE8启用这些CSS3伪类?

For IE8 not as far as I know it. 对于IE8,据我所知。 However there is a solution with jQuery using .filter(":even"). 但是,jQuery使用.filter(":even").有一个解决方案.filter(":even"). see this documentation about it. 请参阅此文档 It will work with IE7 or newer. 它将与IE7或更高版本一起使用。

You can get nth child selectors in IE with a JavaScript shim such as selectivizr . 您可以使用诸如selectivizr之类的JavaScript填充程序在IE中获得第n个子选择器。 This article is also interesting for faking the functionality http://abouthalf.com/2011/07/06/poor-mans-nth-child-selector-for-ie-7-and-8/ . 伪造功能http://abouthalf.com/2011/07/06/poor-mans-nth-child-selector-for-ie-7-and-8/也会使本文有趣。

I have done it by Prototype JS. 我已经通过原型JS完成了。 Works also in IE8. 在IE8中也可以使用。

var rowCounter = 0;
$$('tr').each(function (row) { /*$$('tr') == array of tr tags*/
    if (rowCounter % 2 === 0) { /*modulo dividing find even element*/
        row.addClassName('even');
    }
    rowCounter += 1;
});

Visit: http://jsfiddle.net/P9SHy/3/ 访问: http//jsfiddle.net/P9SHy/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM