javascript - 根据值动态更改 HTML 社区中的字段颜色

标签 javascript html css asp.net

“盈余”和“不足”两栏给了我时间上的差异,问题是我必须以某种方式区分它们。

我是这个主题的新手,重点是如果这两列中的字段值与 00:00 不同,它会将颜色更改为不同的颜色。

但仅限于单个字段,而不是整个列。有人有想法吗?

Edit fiddle - JSFiddle [^]

带有表格 html 的代码

function diff(start, end) {
  start = start.split(":");
  end = end.split(":");
  const startDate = new Date(0, 0, 0, start[0], start[1], 0);
  const endDate = new Date(0, 0, 0, end[0], end[1], 0);
  let diff = endDate.getTime() - startDate.getTime();
  const hours = Math.floor(diff / 1000 / 60 / 60);
  diff -= hours * 1000 * 60 * 60;
  const minutes = Math.floor(diff / 1000 / 60);
  return (hours <= 9 ? "0" : "") + hours + ":" + (minutes <= 9 ? "0" : "") + minutes;
}

function diffMs(start, end) { // this function can help you calculate a difference for a logical purposes
  return +start.split(":").join('') - +end.split(":").join('');
}
document.querySelector('table').addEventListener('change', function(e) {
  const classList = e.target.classList
  if (classList.contains('start') || classList.contains('end')) {
    //retrieve the associated inputs
    const tr = e.target.parentNode.parentNode
    const [start, end, actual, normative, surplus, deficiency] = [...tr.querySelectorAll('.start,.end,.actual,.normative,.surplus,.deficiency')]
    const value = diff(start.value, end.value);
    actual.value = value
    if (diffMs(actual.value, normative.value) >= 0) {
      surplus.value = diff(normative.value, actual.value);
    }
    if (diffMs(actual.value, normative.value) <= 0) {
      deficiency.value = diff(actual.value, normative.value);
    }
  }
})
<table>
  <thead>
    <tr>
      <th>start</th>
      <th>end</th>
      <th>actual</th>
      <th>normative</th>
      <th>surplus</th>
      <th>deficiency</th>
    </tr>
  </thead>
  <tbody>
    <tr class="day">
      <td><input type="time" class="start" id="start_1" value="08:00"></td>
      <td><input type="time" class="end" id="end_1" value="15:00"></td>
      <td><input type="time" class="actual" id="actual_1" value="07:00" readonly></td>
      <td><input type="time" class="normative" id="normative_1" value="08:00" readonly></td>
      <td><input type="time" class="surplus" id="surplus_1" value="00:00" readonly></td>
      <td><input type="time" class="deficiency" id="deficiency_1" value="00:00" readonly></td>
    </tr>

    <tr class="day">
      <td><input type="time" class="start" id="start_2" value="08:00"></td>
      <td><input type="time" class="end" id="end_2" value="17:00"></td>
      <td><input type="time" class="actual" id="actual_2" value="09:00" readonly></td>
      <td><input type="time" class="normative" id="normative_2" value="08:00" readonly></td>
      <td><input type="time" class="surplus" id="surplus_2" value="00:00" readonly></td>
      <td><input type="time" class="deficiency" id="deficiency_2" value="00:00" readonly></td>
    </tr>
  </tbody>
</table>

注释: 我不想使用 id,因为我有超过 30 条记录

两列中的值指的是这种情况下的“盈余”和“不足”

最佳答案

检查这个jsfiddle:https://jsfiddle.net/9v8pjqna/

在检查“剩余”或“效率”的“差异”的情况下, 添加另一个简单的条件来检查它们的值是否不等于 0。 如果是这样,请访问输入的backgroundColor属性并更改它。

if (diffMs(actual.value, normative.value) >= 0) {
  surplus.value = diff(normative.value, actual.value);
  if(surplus.value != 0) {
    surplus.style.backgroundColor = "red";
  }
}
if (diffMs(actual.value, normative.value) <= 0) {
  deficiency.value = diff(actual.value, normative.value);

  if(deficiency.value != 0) {
    deficiency.style.backgroundColor = "red";
  }
}

关于javascript - 根据值动态更改 HTML 社区中的字段颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60137613/

相关文章:

javascript - 带有变量的复杂 Jquery 选择器

php - 更改图像 onclick - Javascript 按钮和 AJAX?

css - 遇到重复图像的问题

css - 背景附件 :fixed; on mobile Safari

html - 两个内联 block 堆栈之间的垂直空间

javascript - TinyMCE宽度和高度不听话!

javascript - 如何在 document.createElement ("span"之后将文本添加到 span 中);?

html - 具有视频高度的div容器

javascript - 在 node.js/express 中处理 <select> 表单参数

javascript - 是否有一个 javascript 库可以读取样式字符串并将适当的样式添加到给定元素?