如何为响应表实现交替行样式?(How to achieve alternating row styles for a responsive table?)

编程入门 行业动态 更新时间:2024-10-07 19:16:43
何为响应表实现交替行样式?(How to achieve alternating row styles for a responsive table?)

我想我需要JavaScript才能解决这个问题,但这就是我需要帮助的原因(我只编辑了现有的JavaScript - 从未创建过它们)。

我有两个并排的条纹表,当在移动设备上查看时,右侧的表格移动到左侧的表格下方,看起来像一个连续的表格。

问题是只有在移动设备上查看表条带时,如果tbody行计数是偶数,我最终在中间有两个连续的行是相同的颜色。

@media only screen and (max-width: 480px) {
    .sizesTableContent {
        display:block !important;
        width:100% !important;
    }
    .hider {
        display: none;
    }
}
.sizesAsterisk {
    width:100%;
    border-collapse: collapse;
    text-align: left;
    font-family: arial, helvetica, sans-serif;
    font-size: 14px;
    font-weight: normal;
}
.hanging {
    text-indent: -0.5em;
    padding-left: 0.5em;
}
.sizesTableContent {
    vertical-align: top;
}
.sizesTwoColumn {
    width:100%;
    border-collapse: collapse;
    border-top: 1px solid #000000;
    border-bottom: 1px solid #000000;
}
.sizes {
    border-collapse: collapse;
    white-space: nowrap;
    width: 100%;
    text-align: center;
    font-family: arial, helvetica, sans-serif;
    font-size: 14px;
    font-weight: normal;
}
.sizes td:first-child {
    text-align: left;
    font-weight: bold;
}
.sizes th {
    border-bottom: 1pt solid #000000;
    vertical-align: bottom;
    font-family: arial, helvetica, sans-serif;
    font-size: 12px;
    font-weight: normal;
}
.sizes th:first-child {
    text-align: left;
}
.sizes tbody tr:hover {
    background: #D2DAE3;
}
.sizes tbody tr:nth-child(odd) {
    background: #ffffcc;
}
.sizes tbody tr:nth-child(odd):hover {
    background: #D2DAE3;
} 
  
<table class="sizesAsterisk">
    <tr>
        <td>
            <table class="sizesTwoColumn">
                <tr>
                    <td class="sizesTableContent">
                        <table class="sizes" cellspacing="0" cellpadding="0">
                            <col width="33.3%">
                                <col width="33.3%">
                                    <col width="33.4%">
                                        <thead>
                                            <tr>
                                                <th>Size in
                                                    <br/>Inches</th>
                                                <th>Lbs.
                                                    <br/>Per Ft</th>
                                                <th>Est. Lbs.
                                                    <br/>Per 20' Bar</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <!--First column size data go between the tbody tags-->
                                            <tr>
                                                <td>1" x 1/4</td>
                                                <td>.620</td>
                                                <td>12.40</td>
                                            </tr>
                                            <tr>
                                                <td>1-1/4 x 5/15</td>
                                                <td>.960</td>
                                                <td>19.20</td>
                                            </tr>
                                            <tr>
                                                <td>1-1/2 x 5/16</td>
                                                <td>1.180</td>
                                                <td>23.60</td>
                                            </tr>
                                        </tbody>
                        </table>
                    </td>
                    <td class="hider" style="border-right: 1px solid #000000" width="14px"></td>
                    <td class="hider" width="14px"></td>
                    <td class="sizesTableContent">
                        <table class="sizes" cellspacing="0" cellpadding="0">
                            <col width="33.3%">
                                <col width="33.3%">
                                    <col width="33.4%">
                                        <thead class="hider">
                                            <tr>
                                                <th>Size in
                                                    <br/>Inches</th>
                                                <th>Lbs.
                                                    <br/>Per Ft</th>
                                                <th>Est. Lbs.
                                                    <br/>Per 20' Bar</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <!--Second column size data go between the tbody tags-->
                                            <tr>
                                                <td>1-1/2 x 7/16</td>
                                                <td>1.560</td>
                                                <td>31.20</td>
                                            </tr>
                                            <tr>
                                                <td>1-3/4 x 7/16<span style="color:red"> *</span>
                                                </td>
                                                <td>1.910</td>
                                                <td>38.20</td>
                                            </tr>
                                            <tr>
                                                <td>2" x 1/2<span style="color:red"> *</span>
                                                </td>
                                                <td>2.587</td>
                                                <td>51.74</td>
                                            </tr>
                                        </tbody>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td class="hanging"><!--Asterisk notes go between the td tags-->
            <span style="color:red">* </span>Also in 10' Lengths.
        </td>
    </tr>
</table> 
  
 

I think I need JavaScript to solve this but that's why I need help (I've only edited existing JavaScript - never created them).

I have two striped tables nest side-by-side that when viewed on a mobile device, the table on the right moves below the table on the left to look as one continuous table.

The problem is with the table striping only when viewed on a mobile device if the tbody row counts are an even number, I end up with two consecutive rows in the middle being the same color.

@media only screen and (max-width: 480px) {
    .sizesTableContent {
        display:block !important;
        width:100% !important;
    }
    .hider {
        display: none;
    }
}
.sizesAsterisk {
    width:100%;
    border-collapse: collapse;
    text-align: left;
    font-family: arial, helvetica, sans-serif;
    font-size: 14px;
    font-weight: normal;
}
.hanging {
    text-indent: -0.5em;
    padding-left: 0.5em;
}
.sizesTableContent {
    vertical-align: top;
}
.sizesTwoColumn {
    width:100%;
    border-collapse: collapse;
    border-top: 1px solid #000000;
    border-bottom: 1px solid #000000;
}
.sizes {
    border-collapse: collapse;
    white-space: nowrap;
    width: 100%;
    text-align: center;
    font-family: arial, helvetica, sans-serif;
    font-size: 14px;
    font-weight: normal;
}
.sizes td:first-child {
    text-align: left;
    font-weight: bold;
}
.sizes th {
    border-bottom: 1pt solid #000000;
    vertical-align: bottom;
    font-family: arial, helvetica, sans-serif;
    font-size: 12px;
    font-weight: normal;
}
.sizes th:first-child {
    text-align: left;
}
.sizes tbody tr:hover {
    background: #D2DAE3;
}
.sizes tbody tr:nth-child(odd) {
    background: #ffffcc;
}
.sizes tbody tr:nth-child(odd):hover {
    background: #D2DAE3;
} 
  
<table class="sizesAsterisk">
    <tr>
        <td>
            <table class="sizesTwoColumn">
                <tr>
                    <td class="sizesTableContent">
                        <table class="sizes" cellspacing="0" cellpadding="0">
                            <col width="33.3%">
                                <col width="33.3%">
                                    <col width="33.4%">
                                        <thead>
                                            <tr>
                                                <th>Size in
                                                    <br/>Inches</th>
                                                <th>Lbs.
                                                    <br/>Per Ft</th>
                                                <th>Est. Lbs.
                                                    <br/>Per 20' Bar</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <!--First column size data go between the tbody tags-->
                                            <tr>
                                                <td>1" x 1/4</td>
                                                <td>.620</td>
                                                <td>12.40</td>
                                            </tr>
                                            <tr>
                                                <td>1-1/4 x 5/15</td>
                                                <td>.960</td>
                                                <td>19.20</td>
                                            </tr>
                                            <tr>
                                                <td>1-1/2 x 5/16</td>
                                                <td>1.180</td>
                                                <td>23.60</td>
                                            </tr>
                                        </tbody>
                        </table>
                    </td>
                    <td class="hider" style="border-right: 1px solid #000000" width="14px"></td>
                    <td class="hider" width="14px"></td>
                    <td class="sizesTableContent">
                        <table class="sizes" cellspacing="0" cellpadding="0">
                            <col width="33.3%">
                                <col width="33.3%">
                                    <col width="33.4%">
                                        <thead class="hider">
                                            <tr>
                                                <th>Size in
                                                    <br/>Inches</th>
                                                <th>Lbs.
                                                    <br/>Per Ft</th>
                                                <th>Est. Lbs.
                                                    <br/>Per 20' Bar</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <!--Second column size data go between the tbody tags-->
                                            <tr>
                                                <td>1-1/2 x 7/16</td>
                                                <td>1.560</td>
                                                <td>31.20</td>
                                            </tr>
                                            <tr>
                                                <td>1-3/4 x 7/16<span style="color:red"> *</span>
                                                </td>
                                                <td>1.910</td>
                                                <td>38.20</td>
                                            </tr>
                                            <tr>
                                                <td>2" x 1/2<span style="color:red"> *</span>
                                                </td>
                                                <td>2.587</td>
                                                <td>51.74</td>
                                            </tr>
                                        </tbody>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td class="hanging"><!--Asterisk notes go between the td tags-->
            <span style="color:red">* </span>Also in 10' Lengths.
        </td>
    </tr>
</table> 
  
 

最满意答案

你不需要JavaScript。 只需在媒体查询中使用一些:last-child伪选择器,即可在移动视图中更多地更改演示文稿。 这实际上只在移动视图中切换第二个表的偶数/奇数背景:

.sizesTableContent:last-child .sizes tbody tr:nth-child(odd) { background: #fff; } .sizesTableContent:last-child .sizes tbody tr:nth-child(even) { background: #ffffcc; }

JSFiddle示例

@media only screen and (max-width: 480px) {
    .sizesTableContent {
        display:block !important;
        width:100% !important;
    }
    .hider {
        display: none;
    }
    .sizesTableContent:last-child .sizes tbody tr:nth-child(odd) {
        background: #fff;
    }
    .sizesTableContent:last-child .sizes tbody tr:nth-child(even) {
        background: #ffffcc;
    }
}
.sizesAsterisk {
    width:100%;
    border-collapse: collapse;
    text-align: left;
    font-family: arial, helvetica, sans-serif;
    font-size: 14px;
    font-weight: normal;
}
.hanging {
    text-indent: -0.5em;
    padding-left: 0.5em;
}
.sizesTableContent {
    vertical-align: top;
}
.sizesTwoColumn {
    width:100%;
    border-collapse: collapse;
    border-top: 1px solid #000000;
    border-bottom: 1px solid #000000;
}
.sizes {
    border-collapse: collapse;
    white-space: nowrap;
    width: 100%;
    text-align: center;
    font-family: arial, helvetica, sans-serif;
    font-size: 14px;
    font-weight: normal;
}
.sizes td:first-child {
    text-align: left;
    font-weight: bold;
}
.sizes th {
    border-bottom: 1pt solid #000000;
    vertical-align: bottom;
    font-family: arial, helvetica, sans-serif;
    font-size: 12px;
    font-weight: normal;
}
.sizes th:first-child {
    text-align: left;
}
.sizes tbody tr:hover {
    background: #D2DAE3;
}
.sizes tbody tr:nth-child(odd) {
    background: #ffffcc;
}
.sizes tbody tr:nth-child(odd):hover {
    background: #D2DAE3;
} 
  
<table class="sizesAsterisk">
    <tr>
        <td>
            <table class="sizesTwoColumn">
                <tr>
                    <td class="sizesTableContent">
                        <table class="sizes" cellspacing="0" cellpadding="0">
                            <col width="33.3%">
                                <col width="33.3%">
                                    <col width="33.4%">
                                        <thead>
                                            <tr>
                                                <th>Size in
                                                    <br/>Inches</th>
                                                <th>Lbs.
                                                    <br/>Per Ft</th>
                                                <th>Est. Lbs.
                                                    <br/>Per 20' Bar</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <!--First column size data go between the tbody tags-->
                                            <tr>
                                                <td>1" x 1/4</td>
                                                <td>.620</td>
                                                <td>12.40</td>
                                            </tr>
                                            <tr>
                                                <td>1-1/4 x 5/15</td>
                                                <td>.960</td>
                                                <td>19.20</td>
                                            </tr>
                                            <tr>
                                                <td>1-1/2 x 5/16</td>
                                                <td>1.180</td>
                                                <td>23.60</td>
                                            </tr>
                                        </tbody>
                        </table>
                    </td>
                    <td class="hider" style="border-right: 1px solid #000000" width="14px"></td>
                    <td class="hider" width="14px"></td>
                    <td class="sizesTableContent">
                        <table class="sizes" cellspacing="0" cellpadding="0">
                            <col width="33.3%">
                                <col width="33.3%">
                                    <col width="33.4%">
                                        <thead class="hider">
                                            <tr>
                                                <th>Size in
                                                    <br/>Inches</th>
                                                <th>Lbs.
                                                    <br/>Per Ft</th>
                                                <th>Est. Lbs.
                                                    <br/>Per 20' Bar</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <!--Second column size data go between the tbody tags-->
                                            <tr>
                                                <td>1-1/2 x 7/16</td>
                                                <td>1.560</td>
                                                <td>31.20</td>
                                            </tr>
                                            <tr>
                                                <td>1-3/4 x 7/16<span style="color:red"> *</span>

                                                </td>
                                                <td>1.910</td>
                                                <td>38.20</td>
                                            </tr>
                                            <tr>
                                                <td>2" x 1/2<span style="color:red"> *</span>

                                                </td>
                                                <td>2.587</td>
                                                <td>51.74</td>
                                            </tr>
                                        </tbody>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td class="hanging">
            <!--Asterisk notes go between the td tags-->
<span style="color:red">* </span>Also in 10' Lengths.</td>
    </tr>
</table> 
  
 

You won't need JavaScript. Simply use some :last-child pseudo-selectors in your media query to change the presentation a little more in mobile view. This essentially switches the even/odd backgrounds of the 2nd table only in mobile view:

.sizesTableContent:last-child .sizes tbody tr:nth-child(odd) { background: #fff; } .sizesTableContent:last-child .sizes tbody tr:nth-child(even) { background: #ffffcc; }

JSFiddle Example

@media only screen and (max-width: 480px) {
    .sizesTableContent {
        display:block !important;
        width:100% !important;
    }
    .hider {
        display: none;
    }
    .sizesTableContent:last-child .sizes tbody tr:nth-child(odd) {
        background: #fff;
    }
    .sizesTableContent:last-child .sizes tbody tr:nth-child(even) {
        background: #ffffcc;
    }
}
.sizesAsterisk {
    width:100%;
    border-collapse: collapse;
    text-align: left;
    font-family: arial, helvetica, sans-serif;
    font-size: 14px;
    font-weight: normal;
}
.hanging {
    text-indent: -0.5em;
    padding-left: 0.5em;
}
.sizesTableContent {
    vertical-align: top;
}
.sizesTwoColumn {
    width:100%;
    border-collapse: collapse;
    border-top: 1px solid #000000;
    border-bottom: 1px solid #000000;
}
.sizes {
    border-collapse: collapse;
    white-space: nowrap;
    width: 100%;
    text-align: center;
    font-family: arial, helvetica, sans-serif;
    font-size: 14px;
    font-weight: normal;
}
.sizes td:first-child {
    text-align: left;
    font-weight: bold;
}
.sizes th {
    border-bottom: 1pt solid #000000;
    vertical-align: bottom;
    font-family: arial, helvetica, sans-serif;
    font-size: 12px;
    font-weight: normal;
}
.sizes th:first-child {
    text-align: left;
}
.sizes tbody tr:hover {
    background: #D2DAE3;
}
.sizes tbody tr:nth-child(odd) {
    background: #ffffcc;
}
.sizes tbody tr:nth-child(odd):hover {
    background: #D2DAE3;
} 
  
<table class="sizesAsterisk">
    <tr>
        <td>
            <table class="sizesTwoColumn">
                <tr>
                    <td class="sizesTableContent">
                        <table class="sizes" cellspacing="0" cellpadding="0">
                            <col width="33.3%">
                                <col width="33.3%">
                                    <col width="33.4%">
                                        <thead>
                                            <tr>
                                                <th>Size in
                                                    <br/>Inches</th>
                                                <th>Lbs.
                                                    <br/>Per Ft</th>
                                                <th>Est. Lbs.
                                                    <br/>Per 20' Bar</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <!--First column size data go between the tbody tags-->
                                            <tr>
                                                <td>1" x 1/4</td>
                                                <td>.620</td>
                                                <td>12.40</td>
                                            </tr>
                                            <tr>
                                                <td>1-1/4 x 5/15</td>
                                                <td>.960</td>
                                                <td>19.20</td>
                                            </tr>
                                            <tr>
                                                <td>1-1/2 x 5/16</td>
                                                <td>1.180</td>
                                                <td>23.60</td>
                                            </tr>
                                        </tbody>
                        </table>
                    </td>
                    <td class="hider" style="border-right: 1px solid #000000" width="14px"></td>
                    <td class="hider" width="14px"></td>
                    <td class="sizesTableContent">
                        <table class="sizes" cellspacing="0" cellpadding="0">
                            <col width="33.3%">
                                <col width="33.3%">
                                    <col width="33.4%">
                                        <thead class="hider">
                                            <tr>
                                                <th>Size in
                                                    <br/>Inches</th>
                                                <th>Lbs.
                                                    <br/>Per Ft</th>
                                                <th>Est. Lbs.
                                                    <br/>Per 20' Bar</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <!--Second column size data go between the tbody tags-->
                                            <tr>
                                                <td>1-1/2 x 7/16</td>
                                                <td>1.560</td>
                                                <td>31.20</td>
                                            </tr>
                                            <tr>
                                                <td>1-3/4 x 7/16<span style="color:red"> *</span>

                                                </td>
                                                <td>1.910</td>
                                                <td>38.20</td>
                                            </tr>
                                            <tr>
                                                <td>2" x 1/2<span style="color:red"> *</span>

                                                </td>
                                                <td>2.587</td>
                                                <td>51.74</td>
                                            </tr>
                                        </tbody>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td class="hanging">
            <!--Asterisk notes go between the td tags-->
<span style="color:red">* </span>Also in 10' Lengths.</td>
    </tr>
</table> 
  
 

更多推荐

本文发布于:2023-08-03 21:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1400885.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:何为   样式   achieve   alternating   table

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!