如何为我的表格行的第n个子元素创建不同的背景?

huangapple go评论64阅读模式
英文:

How to create different background for my Table row nth-child?

问题

我已经使用外部的Grid.js库创建了一个表格。我在表格的样式方面遇到了一些问题。我想要改变除了一行之外的所有行的背景颜色。我使用了以下CSS代码:tr.gridjs-tr:nth-child(even) {background: #47bb60;},但它不起作用。你能提供一些建议,CSS中缺少什么吗?

th {
    box-sizing: border-box;
    background: #1f61cf;
    color: #fff;
}

tr.gridjs-tr:nth-child(even) {
    background: #47bb60;
}

这段CSS代码应该能够实现你想要的效果。希望对你有所帮助。

英文:

I have created a table by using the external grid js library. I am facing some issues in styling the table. I want to change the background color of all the rows by leaving one row. I am using the code tr.gridjs-tr:nth-child(even) {background: #47bb60;} but It's not working. Can you suggest to me, What is missing in the CSS?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

new gridjs.Grid({

    columns: [&quot;Name&quot;, &quot;Email&quot;, &quot;Phone Number&quot;],
    data: [
    [&quot;John&quot;, &quot;john@example.com&quot;, &quot;(353) 01 222 3333&quot;],
    [&quot;Mark&quot;, &quot;mark@gmail.com&quot;, &quot;(01) 22 888 4444&quot;],
    [&quot;Eoin&quot;, &quot;eoin@gmail.com&quot;, &quot;0097 22 654 00033&quot;],
    [&quot;Sarah&quot;, &quot;sarahcdd@gmail.com&quot;, &quot;+322 876 1233&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;]
    ],

    sort: true,
    Default: true,
    style: {
        table: {
            border: &#39;3px solid #ccc&#39;
        },
        th: {
            &#39;background-color&#39;: &#39;rgba(0, 0, 0, 0.1)&#39;,
            &#39;color&#39;: &#39;#fff&#39;,
            &#39;border-bottom&#39;: &#39;3px solid #ccc&#39;,
            &#39;text-align&#39;: &#39;center&#39;
        },
        td: {
            &#39;text-align&#39;: &#39;center&#39;
        },
    }


}).render(document.getElementById(&quot;wrapper&quot;));

<!-- language: lang-css -->

th {
    box-sizing: border-box;
    background: #1f61cf;
    color: #fff;
}

tr.gridjs-tr:nth-child(even) {
    background: #47bb60;
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en-US&quot;&gt;

&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;link href=&quot;style.css&quot; rel=&quot;stylesheet&quot;&gt;
    &lt;link href=&quot;https://unpkg.com/gridjs/dist/theme/mermaid.min.css&quot; rel=&quot;stylesheet&quot; /&gt;

&lt;/head&gt;

&lt;body&gt;
&lt;div id=&quot;wrapper&quot;&gt;&lt;/div&gt;


    &lt;script type=&quot;text/javascript&quot; src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot;
        src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://unpkg.com/gridjs/dist/gridjs.umd.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;js.js&quot;&gt;&lt;/script&gt;

&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 2

如果你想要交替改变行的背景颜色,可以使用 nth-child(even/odd) 选择 tr 元素,然后将背景颜色应用到 td 元素。

th {
    box-sizing: border-box;
    background: #1f61cf;
    color: #fff;
}

.gridjs-tr:nth-child(even) td {
    background: #47bb60;
}

这段CSS代码将奇数行的背景颜色设置为 #47bb60,偶数行的背景颜色保持不变。

英文:

If you want to change the row color alternatively, select the tr using nth-child(even/odd) and then apply the background color to td

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

new gridjs.Grid({

    columns: [&quot;Name&quot;, &quot;Email&quot;, &quot;Phone Number&quot;],
    data: [
    [&quot;John&quot;, &quot;john@example.com&quot;, &quot;(353) 01 222 3333&quot;],
    [&quot;Mark&quot;, &quot;mark@gmail.com&quot;, &quot;(01) 22 888 4444&quot;],
    [&quot;Eoin&quot;, &quot;eoin@gmail.com&quot;, &quot;0097 22 654 00033&quot;],
    [&quot;Sarah&quot;, &quot;sarahcdd@gmail.com&quot;, &quot;+322 876 1233&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
    [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;]
    ],

    sort: true,
    Default: true,
    style: {
        table: {
            border: &#39;3px solid #ccc&#39;
        },
        th: {
            &#39;background-color&#39;: &#39;rgba(0, 0, 0, 0.1)&#39;,
            &#39;color&#39;: &#39;#fff&#39;,
            &#39;border-bottom&#39;: &#39;3px solid #ccc&#39;,
            &#39;text-align&#39;: &#39;center&#39;
        },
        td: {
            &#39;text-align&#39;: &#39;center&#39;
        },
    }


}).render(document.getElementById(&quot;wrapper&quot;));

<!-- language: lang-css -->

th {
    box-sizing: border-box;
    background: #1f61cf;
    color: #fff;
}

.gridjs-tr:nth-child(even) td {
    background: #47bb60;
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en-US&quot;&gt;

&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;link href=&quot;style.css&quot; rel=&quot;stylesheet&quot;&gt;
    &lt;link href=&quot;https://unpkg.com/gridjs/dist/theme/mermaid.min.css&quot; rel=&quot;stylesheet&quot; /&gt;

&lt;/head&gt;

&lt;body&gt;
&lt;div id=&quot;wrapper&quot;&gt;&lt;/div&gt;


    &lt;script type=&quot;text/javascript&quot; src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot;
        src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://unpkg.com/gridjs/dist/gridjs.umd.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;js.js&quot;&gt;&lt;/script&gt;

&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月19日 23:14:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定