1

我有一个动态表,我想在替代行上赋予颜色。我怎样才能用css实现这一点?我需要代码才能在 IE7+ 中工作

4

4 回答 4

3

研究在 CSS3 中使用偶数/奇数规则。

参考https ://developer.mozilla.org/en/CSS/:nth-child

例如,

tr:nth-child(odd)将代表每个2n + 1孩子的 CSS,而tr:nth-child(even)将代表每个2n孩子的 CSS。

于 2012-07-16T14:36:29.180 回答
2

CSS3 nth-child 选择器:

tr:nth-child(odd) {
  background: red /* or whatever */;
}
于 2012-07-16T14:36:49.107 回答
2

您可以使用CSS3 选择器

tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}

jQuery

$("tr:even").css("background-color", "#CCC");
$("tr:odd").css("background-color", "#FFF");

或者在服务器端进行。

于 2012-07-16T14:47:22.093 回答
2

星期五我遇到了同样的问题,我使用了 jquery 解决方案

$("tr:even").css("background-color", "#CCC");
$("tr:odd").css("background-color", "#FFF");

所以基本上你在头部添加 .js 脚本并触发 dom 更改的 jquery 规则。

我完成的 .js 看起来像这样

<script type="text/javascript">
    (function (window) {

        var last = +new Date();
        var delay = 100; // default delay

        // Manage event queue
        var stack = [];

        function callback() {
            var now = +new Date();
            if (now - last > delay) {
                for (var i = 0; i < stack.length; i++) {
                    stack[i]();
                }
                last = now;
            }
        }

        // Public interface
        var onDomChange = function (fn, newdelay) {
            if (newdelay)
                delay = newdelay;
            stack.push(fn);
        };

        // Naive approach for compatibility
        function naive() {

            var last = document.getElementsByTagName('*');
            var lastlen = last.length;
            var timer = setTimeout(function check() {

                // get current state of the document
                var current = document.getElementsByTagName('*');
                var len = current.length;

                // if the length is different
                // it's fairly obvious
                if (len != lastlen) {
                    // just make sure the loop finishes early
                    last = [];
                }

                // go check every element in order
                for (var i = 0; i < len; i++) {
                    if (current[i] !== last[i]) {
                        callback();
                        last = current;
                        lastlen = len;
                        break;
                    }
                }

                // over, and over, and over again
                setTimeout(check, delay);

            }, delay);
        }

        //
        //  Check for mutation events support
        //

        var support = {};

        var el = document.documentElement;
        var remain = 3;

        // callback for the tests
        function decide() {
            if (support.DOMNodeInserted) {
                window.addEventListener("DOMContentLoaded", function () {
                    if (support.DOMSubtreeModified) { // for FF 3+, Chrome
                        el.addEventListener('DOMSubtreeModified', callback, false);
                    } else { // for FF 2, Safari, Opera 9.6+
                        el.addEventListener('DOMNodeInserted', callback, false);
                        el.addEventListener('DOMNodeRemoved', callback, false);
                    }
                }, false);
            } else if (document.onpropertychange) { // for IE 5.5+
                document.onpropertychange = callback;
            } else { // fallback
                naive();
            }
        }

        // checks a particular event
        function test(event) {
            el.addEventListener(event, function fn() {
                support[event] = true;
                el.removeEventListener(event, fn, false);
                if (--remain === 0) decide();
            }, false);
        }

        // attach test events
        if (window.addEventListener) {
            test('DOMSubtreeModified');
            test('DOMNodeInserted');
            test('DOMNodeRemoved');
        } else {
            decide();
        }

        // do the dummy test
        var dummy = document.createElement("div");
        el.appendChild(dummy);
        el.removeChild(dummy);

        // expose
        window.onDomChange = onDomChange;

    })(window);



    $(document).ready(function () {
        $("tr:even").css("background-color", "#CCC");
        $("tr:odd").css("background-color", "#FFF");
        onDomChange(function () {
            $("tr:even").css("background-color", "#CCC");
            $("tr:odd").css("background-color", "#FFF");
        });
    });
</script>    

我想提醒这个答案,这可能不是最好的解决方案,但可以满足我的需要。:-)

于 2012-07-16T15:20:26.500 回答