仅在嵌套的顶部表上使用tr:nth子级(奇数)和tr:nh子级(偶数)选择器


Use tr:nth-child(odd) and tr:nth-child(even) selectors only on nested top tables

我想要一张斑马纹表
数据是从数据库中提取的,每个实例都会创建一个新表
我想要<table>级别的斑马线。这将意味着每隔一个CCD_ 2元素获得不同的颜色。

我试图在我的<table class="oddeven">中添加一个类,但这仍然会对每个tr.进行更改

这是我使用它的代码:

<?php
                global $wpdb;
                $sql = "SELECT * FROM $wpdb->postmeta WHERE meta_key = 'group' AND meta_value='$group'";
                $results = $wpdb->get_results($sql) or die(mysql_error());
                echo'<table cellpadding="0" cellspacing="0" border="0" width="100%">';
                    foreach( $results as $result ) 
                    {       
                        $post_id = $result->post_id;
                        $company_name = get_post_meta($post_id, 'company_name', true); 
                        $address = get_post_meta($post_id, 'address', true); 
                        $postal_code = get_post_meta($post_id, 'postal_code', true); 
                        $city = get_post_meta($post_id, 'city', true); 
                        $phone = get_post_meta($post_id, 'phone', true); 
                        echo '
                        <tr>
                            <td>
                                <table cellpadding="0" cellspacing="0" border="0" width="100%" class="oddeven">
                                    <tr>
                                        <td width="75%">
                                            <strong>'.$company_name.'</strong>
                                        </td>
                                        <td rowspan="4"><img class="table_image" src="'.get_bloginfo('template_directory').'/images/arrow.png"></td>
                                    </tr>
                                    <tr>
                                        <td>
                                            '.$address.'
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            '.$postal_code.'  '.$city.'
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            '.$phone.'
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>';
                    }
                    echo '</table>';
            ?>

这是样式:(在打字时,我意识到这是在tr:level上)

.oddeven tr:nth-child(odd){ 
background: #ffffff;
}
.oddeven tr:nth-child(even){
background: #000000;
}   

我希望我能澄清

如果您将foreach循环更改为如下的for循环:

for($i = 0; $i < count($results); $i++) {
    $result = $results[$i];
    ...

然后,您可以通过测试$i % 2 == 0来判断当前行是偶数还是奇数。如果计算结果为true,则添加一个"even"类;否则添加一个"奇数"类。

如果您不想将更改传播到子表,您也可以在子表上强制使用相同的颜色:

.top tr:nth-child(odd) {
    background-color: red;
}   
.top tr:nth-child(odd) tr {
    background-color: red;
}
.top tr:nth-child(even) {
    background-color: yellow;
}   
.top tr:nth-child(even) tr {
    background-color: yellow;
}

我想这正是您想要的,因为您已经剥离了嵌套表上的行或者我弄错了,您可能需要解释<table>级别是什么,因为您已经嵌套了表。