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

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

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

问题

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

  1. th {
  2. box-sizing: border-box;
  3. background: #1f61cf;
  4. color: #fff;
  5. }
  6. tr.gridjs-tr:nth-child(even) {
  7. background: #47bb60;
  8. }

这段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 -->

  1. new gridjs.Grid({
  2. columns: [&quot;Name&quot;, &quot;Email&quot;, &quot;Phone Number&quot;],
  3. data: [
  4. [&quot;John&quot;, &quot;john@example.com&quot;, &quot;(353) 01 222 3333&quot;],
  5. [&quot;Mark&quot;, &quot;mark@gmail.com&quot;, &quot;(01) 22 888 4444&quot;],
  6. [&quot;Eoin&quot;, &quot;eoin@gmail.com&quot;, &quot;0097 22 654 00033&quot;],
  7. [&quot;Sarah&quot;, &quot;sarahcdd@gmail.com&quot;, &quot;+322 876 1233&quot;],
  8. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  9. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  10. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  11. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  12. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  13. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  14. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  15. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  16. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  17. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  18. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  19. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  20. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  21. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  22. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  23. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  24. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  25. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  26. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;]
  27. ],
  28. sort: true,
  29. Default: true,
  30. style: {
  31. table: {
  32. border: &#39;3px solid #ccc&#39;
  33. },
  34. th: {
  35. &#39;background-color&#39;: &#39;rgba(0, 0, 0, 0.1)&#39;,
  36. &#39;color&#39;: &#39;#fff&#39;,
  37. &#39;border-bottom&#39;: &#39;3px solid #ccc&#39;,
  38. &#39;text-align&#39;: &#39;center&#39;
  39. },
  40. td: {
  41. &#39;text-align&#39;: &#39;center&#39;
  42. },
  43. }
  44. }).render(document.getElementById(&quot;wrapper&quot;));

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

  1. th {
  2. box-sizing: border-box;
  3. background: #1f61cf;
  4. color: #fff;
  5. }
  6. tr.gridjs-tr:nth-child(even) {
  7. background: #47bb60;
  8. }

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

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en-US&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  6. &lt;link href=&quot;style.css&quot; rel=&quot;stylesheet&quot;&gt;
  7. &lt;link href=&quot;https://unpkg.com/gridjs/dist/theme/mermaid.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
  8. &lt;/head&gt;
  9. &lt;body&gt;
  10. &lt;div id=&quot;wrapper&quot;&gt;&lt;/div&gt;
  11. &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;
  12. &lt;script type=&quot;text/javascript&quot;
  13. src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js&quot;&gt;&lt;/script&gt;
  14. &lt;script src=&quot;https://unpkg.com/gridjs/dist/gridjs.umd.js&quot;&gt;&lt;/script&gt;
  15. &lt;script type=&quot;text/javascript&quot; src=&quot;js.js&quot;&gt;&lt;/script&gt;
  16. &lt;/body&gt;
  17. &lt;/html&gt;

<!-- end snippet -->

答案1

得分: 2

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

  1. th {
  2. box-sizing: border-box;
  3. background: #1f61cf;
  4. color: #fff;
  5. }
  6. .gridjs-tr:nth-child(even) td {
  7. background: #47bb60;
  8. }

这段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 -->

  1. new gridjs.Grid({
  2. columns: [&quot;Name&quot;, &quot;Email&quot;, &quot;Phone Number&quot;],
  3. data: [
  4. [&quot;John&quot;, &quot;john@example.com&quot;, &quot;(353) 01 222 3333&quot;],
  5. [&quot;Mark&quot;, &quot;mark@gmail.com&quot;, &quot;(01) 22 888 4444&quot;],
  6. [&quot;Eoin&quot;, &quot;eoin@gmail.com&quot;, &quot;0097 22 654 00033&quot;],
  7. [&quot;Sarah&quot;, &quot;sarahcdd@gmail.com&quot;, &quot;+322 876 1233&quot;],
  8. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  9. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  10. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  11. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  12. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  13. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  14. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  15. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  16. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  17. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  18. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  19. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  20. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  21. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  22. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  23. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  24. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  25. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;],
  26. [&quot;Afshin&quot;, &quot;afshin@mail.com&quot;, &quot;(353) 22 87 8356&quot;]
  27. ],
  28. sort: true,
  29. Default: true,
  30. style: {
  31. table: {
  32. border: &#39;3px solid #ccc&#39;
  33. },
  34. th: {
  35. &#39;background-color&#39;: &#39;rgba(0, 0, 0, 0.1)&#39;,
  36. &#39;color&#39;: &#39;#fff&#39;,
  37. &#39;border-bottom&#39;: &#39;3px solid #ccc&#39;,
  38. &#39;text-align&#39;: &#39;center&#39;
  39. },
  40. td: {
  41. &#39;text-align&#39;: &#39;center&#39;
  42. },
  43. }
  44. }).render(document.getElementById(&quot;wrapper&quot;));

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

  1. th {
  2. box-sizing: border-box;
  3. background: #1f61cf;
  4. color: #fff;
  5. }
  6. .gridjs-tr:nth-child(even) td {
  7. background: #47bb60;
  8. }

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

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en-US&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  6. &lt;link href=&quot;style.css&quot; rel=&quot;stylesheet&quot;&gt;
  7. &lt;link href=&quot;https://unpkg.com/gridjs/dist/theme/mermaid.min.css&quot; rel=&quot;stylesheet&quot; /&gt;
  8. &lt;/head&gt;
  9. &lt;body&gt;
  10. &lt;div id=&quot;wrapper&quot;&gt;&lt;/div&gt;
  11. &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;
  12. &lt;script type=&quot;text/javascript&quot;
  13. src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js&quot;&gt;&lt;/script&gt;
  14. &lt;script src=&quot;https://unpkg.com/gridjs/dist/gridjs.umd.js&quot;&gt;&lt;/script&gt;
  15. &lt;script type=&quot;text/javascript&quot; src=&quot;js.js&quot;&gt;&lt;/script&gt;
  16. &lt;/body&gt;
  17. &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:

确定