grz*_*zes 3 css jquery recursive-descent
有没有办法用jQuery替换下面的CSS?
.quote-body .quote-body { background: #f5f5f5 }
.quote-body .quote-body .quote-body { background: #fff }
.quote-body .quote-body .quote-body .quote-body { background: #f5f5f5 }
.quote-body .quote-body .quote-body .quote-body .quote-body { background: #fff }
...
and so on
Run Code Online (Sandbox Code Playgroud)
像这样的东西可能会起作用:
$('.quote-body').each(function() {
$(this).addClass($(this).parents('.quote-body').size() % 2 ? 'odd' : 'even');
});
Run Code Online (Sandbox Code Playgroud)
对于每个.quotebody元素,找到具有类.quotebody的父元素的数量,并取两个模数来定义奇数或偶数.
编辑
经过测试和工作精美.对于每个元素,这个方法在复杂文档上可能有点迟缓,jQuery必须一直遍历到根元素.但它的确有效.
工作示例:http://www.ulmanen.fi/stuff/oddevenchild.php