根据数值,找到下一个单元格的总数并着色。

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

Depending on the numerical value, find the total number of the next cell and color it

问题

以下是您要翻译的代码部分:

  1. function getNumber(el) {
  2. return parseFloat(el.textContent);
  3. }
  4. const rows = document.querySelectorAll('tbody tr');
  5. const total = getNumber(document.querySelector('#money'));
  6. rows.forEach(row => {
  7. const sum = row.querySelector('td:nth-child(2)');
  8. const tick = row.querySelector('td:nth-child(3)');
  9. const upcoming = row.querySelector('td:nth-child(3)');
  10. if (getNumber(sum) <= total) {
  11. row.classList.add('green');
  12. tick.classList.add('tick');
  13. }
  14. if (getNumber(sum) > total) {
  15. row.classList.add('purple');
  16. upcoming.classList.add('upcoming');
  17. }
  18. });
  1. table {
  2. width: 75%;
  3. top: 0;
  4. }
  5. th {
  6. background-color: #193d49;
  7. color: #f1f1f1;
  8. font-size: 15px;
  9. padding: 4px;
  10. }
  11. tbody tr {
  12. background-color: #376282;
  13. }
  14. td {
  15. color: #fff;
  16. font-size: 15px;
  17. padding: 4px;
  18. text-align: center;
  19. }
  20. #money {
  21. background-color: gold;
  22. color: blue;
  23. font-size: 15px;
  24. padding: 4px;
  25. text-align: center;
  26. }
  27. td:nth-of-type(2):before { content: "€"; }
  28. .green {background-color: green; }
  29. .blue {background-color: #003366; color: #fff; }
  30. .purple {background-color: purple; color: #fff; }
  31. .tick:after { content: '✓'; }
  32. .upcoming:after { content: 'Upcoming...'; }
  1. <table id="table-id">
  2. <thead>
  3. <tr>
  4. <th>A/A</th>
  5. <th>MONEY</th>
  6. <th>GOAL</th>
  7. </tr>
  8. </thead>
  9. <tbody>
  10. <tr>
  11. <td>1</td>
  12. <td>100</td>
  13. <td></td>
  14. </tr>
  15. <tr>
  16. <td>2</td>
  17. <td>200</td>
  18. <td></td>
  19. </tr>
  20. <tr>
  21. <td>3</td>
  22. <td>500</td>
  23. <td></td>
  24. </tr>
  25. <tr>
  26. <td>4</td>
  27. <td>1000</td>
  28. <td></td>
  29. </tr>
  30. <tr>
  31. <td>5</td>
  32. <td>5000</td>
  33. <td></td>
  34. </tr>
  35. </tbody>
  36. </table>
  37. <h3>Total €<span id="money">200</span></h3>
英文:

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

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

  1. function getNumber(el) {
  2. return parseFloat(el.textContent);
  3. }
  4. const rows = document.querySelectorAll(&#39;tbody tr&#39;);
  5. const total = getNumber(document.querySelector(&#39;#money&#39;));
  6. rows.forEach(row =&gt; {
  7. const sum = row.querySelector(&#39;td:nth-child(2)&#39;);
  8. const tick = row.querySelector(&#39;td:nth-child(3)&#39;);
  9. const upcoming = row.querySelector(&#39;td:nth-child(3)&#39;);
  10. if (getNumber(sum) &lt;= total) {
  11. row.classList.add(&#39;green&#39;);
  12. tick.classList.add(&#39;tick&#39;);
  13. }
  14. if (getNumber(sum) &gt; total) {
  15. row.classList.add(&#39;purple&#39;);
  16. upcoming.classList.add(&#39;upcoming&#39;);
  17. }
  18. });

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

  1. table {
  2. width: 75%;
  3. top: 0;
  4. }
  5. th {
  6. background-color: #193d49;
  7. color: #f1f1f1;
  8. font-size: 15px;
  9. padding: 4px;
  10. }
  11. tbody tr {
  12. background-color: #376282;
  13. }
  14. td {
  15. color: #fff;
  16. font-size: 15px;
  17. padding: 4px;
  18. text-align: center;
  19. }
  20. #money {
  21. background-color: gold;
  22. color: blue;
  23. font-size: 15px;
  24. padding: 4px;
  25. text-align: center;
  26. }
  27. td:nth-of-type(2):before { content: &quot;€&quot;; }
  28. .green {background-color: green; }
  29. .blue {background-color: #003366; color: #fff; }
  30. .purple {background-color: purple; color: #fff; }
  31. .tick:after { content: &#39;✓&#39;; }
  32. .upcoming:after { content: &#39;Upcoming...&#39;; }

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

  1. &lt;table id=&quot;table-id&quot;&gt;
  2. &lt;thead&gt;
  3. &lt;tr&gt;
  4. &lt;th&gt;A/A&lt;/th&gt;
  5. &lt;th&gt;MONEY&lt;/th&gt;
  6. &lt;th&gt;GOAL&lt;/th&gt;
  7. &lt;/tr&gt;
  8. &lt;/thead&gt;
  9. &lt;tbody&gt;
  10. &lt;tr&gt;
  11. &lt;td&gt;1&lt;/td&gt;
  12. &lt;td&gt;100&lt;/td&gt;
  13. &lt;td&gt;&lt;/td&gt;
  14. &lt;/tr&gt;
  15. &lt;tr&gt;
  16. &lt;td&gt;2&lt;/td&gt;
  17. &lt;td&gt;200&lt;/td&gt;
  18. &lt;td&gt;&lt;/td&gt;
  19. &lt;/tr&gt;
  20. &lt;tr&gt;
  21. &lt;td&gt;3&lt;/td&gt;
  22. &lt;td&gt;500&lt;/td&gt;
  23. &lt;td&gt;&lt;/td&gt;
  24. &lt;/tr&gt;
  25. &lt;tr&gt;
  26. &lt;td&gt;4&lt;/td&gt;
  27. &lt;td&gt;1000&lt;/td&gt;
  28. &lt;td&gt;&lt;/td&gt;
  29. &lt;/tr&gt;
  30. &lt;tr&gt;
  31. &lt;td&gt;5&lt;/td&gt;
  32. &lt;td&gt;5000&lt;/td&gt;
  33. &lt;td&gt;&lt;/td&gt;
  34. &lt;/tr&gt;
  35. &lt;/tbody&gt;
  36. &lt;/table&gt;
  37. &lt;h3&gt;Total €&lt;span id=&quot;money&quot;&gt;200&lt;/span&gt;&lt;/h3&gt;

<!-- end snippet -->

When I achieve a money goal, the corresponding rows is colored green with a check symbol.

This is definitely done manually. For example if I write 100 on the table, the 100 goal is completed.

  1. What I want is, if I write for example 98, to write in the goal column "Remain €2 to complete".

And this should happen for every goal, 200, 500, 1000, 5000. If I write 420 =&gt; "Remain €80 to complete"

  1. Also, in the exact next cells where the target has not been completed yet, I want it to have the blue color and not the colors that are in the upcoming cells (photo).

Is it possible;

根据数值,找到下一个单元格的总数并着色。

  1. function getNumber(el) {
  2. return parseFloat(el.textContent);
  3. }
  4. const rows = document.querySelectorAll(&#39;tbody tr&#39;);
  5. const total = getNumber(document.querySelector(&#39;#money&#39;));
  6. rows.forEach(row =&gt; {
  7. const sum = row.querySelector(&#39;td:nth-child(2)&#39;);
  8. const tick = row.querySelector(&#39;td:nth-child(3)&#39;);
  9. const upcoming = row.querySelector(&#39;td:nth-child(3)&#39;);
  10. if (getNumber(sum) &lt;= total) {
  11. row.classList.add(&#39;green&#39;);
  12. tick.classList.add(&#39;tick&#39;);
  13. }
  14. if (getNumber(sum) &gt; total) {
  15. row.classList.add(&#39;purple&#39;);
  16. upcoming.classList.add(&#39;upcoming&#39;);
  17. }
  18. });
  19. table {
  20. width: 75%;
  21. top: 0;
  22. }
  23. th {
  24. background-color: #193d49;
  25. color: #f1f1f1;
  26. font-size: 15px;
  27. padding: 4px;
  28. }
  29. tbody tr {
  30. background-color: #376282;
  31. }
  32. td {
  33. color: #fff;
  34. font-size: 15px;
  35. padding: 4px;
  36. text-align: center;
  37. }
  38. #money {
  39. background-color: gold;
  40. color: blue;
  41. font-size: 15px;
  42. padding: 4px;
  43. text-align: center;
  44. }
  45. td:nth-of-type(2):before { content: &quot;€&quot;; }
  46. .green {background-color: green; }
  47. .blue {background-color: #003366; color: #fff; }
  48. .purple {background-color: purple; color: #fff; }
  49. .tick:after { content: &#39;✓&#39;; }
  50. .upcoming:after { content: &#39;Upcoming...&#39;; }
  51. &lt;table id=&quot;table-id&quot;&gt;
  52. &lt;thead&gt;
  53. &lt;tr&gt;
  54. &lt;th&gt;A/A&lt;/th&gt;
  55. &lt;th&gt;MONEY&lt;/th&gt;
  56. &lt;th&gt;GOAL&lt;/th&gt;
  57. &lt;/tr&gt;
  58. &lt;/thead&gt;
  59. &lt;tbody&gt;
  60. &lt;tr&gt;
  61. &lt;td&gt;1&lt;/td&gt;
  62. &lt;td&gt;100&lt;/td&gt;
  63. &lt;td&gt;&lt;/td&gt;
  64. &lt;/tr&gt;
  65. &lt;tr&gt;
  66. &lt;td&gt;2&lt;/td&gt;
  67. &lt;td&gt;200&lt;/td&gt;
  68. &lt;td&gt;&lt;/td&gt;
  69. &lt;/tr&gt;
  70. &lt;tr&gt;
  71. &lt;td&gt;3&lt;/td&gt;
  72. &lt;td&gt;500&lt;/td&gt;
  73. &lt;td&gt;&lt;/td&gt;
  74. &lt;/tr&gt;
  75. &lt;tr&gt;
  76. &lt;td&gt;4&lt;/td&gt;
  77. &lt;td&gt;1000&lt;/td&gt;
  78. &lt;td&gt;&lt;/td&gt;
  79. &lt;/tr&gt;
  80. &lt;tr&gt;
  81. &lt;td&gt;5&lt;/td&gt;
  82. &lt;td&gt;5000&lt;/td&gt;
  83. &lt;td&gt;&lt;/td&gt;
  84. &lt;/tr&gt;
  85. &lt;/tbody&gt;
  86. &lt;/table&gt;
  87. &lt;h3&gt;Total €&lt;span id=&quot;money&quot;&gt;200&lt;/span&gt;&lt;/h3&gt;

答案1

得分: 0

"When the Money column is more than the total, use a variable to indicate whether this is the first row like this. If it is, display the remaining amount instead of the checkbox."

  1. function getNumber(el) {
  2. return parseFloat(el.textContent);
  3. }
  4. const rows = document.querySelectorAll('tbody tr');
  5. const total = getNumber(document.querySelector('#money'));
  6. let firstRemaining = true;
  7. rows.forEach(row => {
  8. const sum = row.querySelector('td:nth-child(2)');
  9. const tick = row.querySelector('td:nth-child(3)');
  10. const upcoming = row.querySelector('td:nth-child(3)');
  11. const amount = getNumber(sum);
  12. if (amount <= total) {
  13. row.classList.add('green');
  14. tick.classList.add('tick');
  15. } else if (firstRemaining) {
  16. tick.innerText = `Remain €${amount-total} to Complete`;
  17. row.classList.add("blue");
  18. firstRemaining = false;
  19. } else {
  20. row.classList.add('purple');
  21. upcoming.classList.add('upcoming');
  22. }
  23. });

I've provided the JavaScript code translation as requested.

英文:

When the Money column is more than the total, use a variable to indicate whether this is the first row like this. If it is, display the remaining amount instead of the checkbox.

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

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

  1. function getNumber(el) {
  2. return parseFloat(el.textContent);
  3. }
  4. const rows = document.querySelectorAll(&#39;tbody tr&#39;);
  5. const total = getNumber(document.querySelector(&#39;#money&#39;));
  6. let firstRemaining = true;
  7. rows.forEach(row =&gt; {
  8. const sum = row.querySelector(&#39;td:nth-child(2)&#39;);
  9. const tick = row.querySelector(&#39;td:nth-child(3)&#39;);
  10. const upcoming = row.querySelector(&#39;td:nth-child(3)&#39;);
  11. const amount = getNumber(sum);
  12. if (amount &lt;= total) {
  13. row.classList.add(&#39;green&#39;);
  14. tick.classList.add(&#39;tick&#39;);
  15. } else if (firstRemaining) {
  16. tick.innerText = `Remain €${amount-total} to Complete`;
  17. row.classList.add(&quot;blue&quot;);
  18. firstRemaining = false;
  19. } else {
  20. row.classList.add(&#39;purple&#39;);
  21. upcoming.classList.add(&#39;upcoming&#39;);
  22. }
  23. });

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

  1. table {
  2. width: 75%;
  3. top: 0;
  4. }
  5. th {
  6. background-color: #193d49;
  7. color: #f1f1f1;
  8. font-size: 15px;
  9. padding: 4px;
  10. }
  11. tbody tr {
  12. background-color: #376282;
  13. }
  14. td {
  15. color: #fff;
  16. font-size: 15px;
  17. padding: 4px;
  18. text-align: center;
  19. }
  20. #money {
  21. background-color: gold;
  22. color: blue;
  23. font-size: 15px;
  24. padding: 4px;
  25. text-align: center;
  26. }
  27. td:nth-of-type(2):before {
  28. content: &quot;€&quot;;
  29. }
  30. .green {
  31. background-color: green;
  32. }
  33. .blue {
  34. background-color: #003366;
  35. color: #fff;
  36. }
  37. .purple {
  38. background-color: purple;
  39. color: #fff;
  40. }
  41. .tick:after {
  42. content: &#39;✓&#39;;
  43. }
  44. .upcoming:after {
  45. content: &#39;Upcoming...&#39;;
  46. }

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

  1. &lt;table id=&quot;table-id&quot;&gt;
  2. &lt;thead&gt;
  3. &lt;tr&gt;
  4. &lt;th&gt;A/A&lt;/th&gt;
  5. &lt;th&gt;MONEY&lt;/th&gt;
  6. &lt;th&gt;GOAL&lt;/th&gt;
  7. &lt;/tr&gt;
  8. &lt;/thead&gt;
  9. &lt;tbody&gt;
  10. &lt;tr&gt;
  11. &lt;td&gt;1&lt;/td&gt;
  12. &lt;td&gt;100&lt;/td&gt;
  13. &lt;td&gt;&lt;/td&gt;
  14. &lt;/tr&gt;
  15. &lt;tr&gt;
  16. &lt;td&gt;2&lt;/td&gt;
  17. &lt;td&gt;200&lt;/td&gt;
  18. &lt;td&gt;&lt;/td&gt;
  19. &lt;/tr&gt;
  20. &lt;tr&gt;
  21. &lt;td&gt;3&lt;/td&gt;
  22. &lt;td&gt;500&lt;/td&gt;
  23. &lt;td&gt;&lt;/td&gt;
  24. &lt;/tr&gt;
  25. &lt;tr&gt;
  26. &lt;td&gt;4&lt;/td&gt;
  27. &lt;td&gt;1000&lt;/td&gt;
  28. &lt;td&gt;&lt;/td&gt;
  29. &lt;/tr&gt;
  30. &lt;tr&gt;
  31. &lt;td&gt;5&lt;/td&gt;
  32. &lt;td&gt;5000&lt;/td&gt;
  33. &lt;td&gt;&lt;/td&gt;
  34. &lt;/tr&gt;
  35. &lt;/tbody&gt;
  36. &lt;/table&gt;
  37. &lt;h3&gt;Total €&lt;span id=&quot;money&quot;&gt;200&lt;/span&gt;&lt;/h3&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月18日 18:39:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500111.html
匿名

发表评论

匿名网友

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

确定