根据表中没有表标记的文本更改表格单元格文本颜色和行背景

时间:2017-11-11 22:09:22

标签: javascript html css dom

我有一个包含状态列的表。两个状态“打开”和“关闭”位于表的最后一列。

我想将“已关闭”的单元格文本颜色更改为红色,将“打开”的行背景颜色更改为绿色。

任何建议都会有所帮助。

编辑:我想了解如何使用javascript在表格的最后一栏中根据文本上下文(打开,关闭)分配上述颜色。

HTML:

<div id="table">
    <div class="row">
        <div class="cell">
            <div class="dataText">a</div>
        </div>
        <div class="cell">
            <div class="dataText">b</div>
        </div>
        <div class="cell">
            <div class="dataText">c</div>
        </div>
        <div class="cell">
            <div class="dataText">Open</div>
        </div>
    </div>
    <div class="row">
        <div class="cell">
            <div class="dataText">1</div>
        </div>
        <div class="cell">
            <div class="dataText">2</div>
        </div>
        <div class="cell">
            <div class="dataText">3</div>
        </div>
        <div class="cell">
            <div class="dataText">Closed</div>
        </div>
    </div>
    <div class="row">
        <div class="cell">
            <div class="dataText">c</div>
        </div>
        <div class="cell">
            <div class="dataText">d</div>
        </div>
        <div class="cell">
            <div class="dataText">e</div>
        </div>
        <div class="cell">
            <div class="dataText">Closed</div>
        </div>
    </div>
    <div class="row">
        <div class="cell">
            <div class="dataText">a</div>
        </div>
        <div class="cell">
            <div class="dataText">b</div>
        </div>
        <div class="cell">
            <div class="dataText">c</div>
        </div>
        <div class="cell">
            <div class="dataText">Open</div>
        </div>
    </div>
</div>

CSS:

#table {
    display: table;
}
.row {
    display: table-row;  
}
.cell {
    display: table-cell;
    padding: 15px;
}

4 个答案:

答案 0 :(得分:0)

容易!

<div class="row open">

.row.open { background: green; }

表示行,

<div class="cell closed">

.dataText.closed { color: red; }

单元格

一个快速的脏(!)解决方案,迭代每一行并动态添加类:

var rows = document.querySelectorAll("div#table .row"); // get all rows
[].forEach.call(rows, function(row) { // iterate over each row
  var cell = row.querySelector(".cell:last-child .dataText"); // get the dataText Element in the last cell in each row
  var cellContent = cell.innerHTML; // read out cell content
  if (cellContent === "Open") { // it says "Open"
      row.classList.add("open"); // add "open" class to row
      cell.classList.add("open"); // add "open" class to status cell
  } else if (cellContent === "Closed") { // it says "Closed"
      row.classList.add("closed"); // add "closed" class to row
      cell.classList.add("closed");// add "closed" class to status cell
  }
});

未经测试,但应该有效。

答案 1 :(得分:0)

您可以使用 document.querySelctorAll() 以及此选择器.cell:last-child .dataText来获取所有最后一列,检查其文本内容,并相应地更改其背景颜色。

这应该是你的代码:

Array.from(document.querySelectorAll(".cell:last-child .dataText")).forEach(function(cell) {
  //We select the whole row with cell.parentNode.parentNode
  if (cell.textContent == "Open")
    cell.parentNode.parentNode.style.backgroundColor = "green";
  else if (cell.textContent == "Closed")
    cell.style.color = "red";
});

<强>演示:

这是一个现场工作演示:

&#13;
&#13;
Array.from(document.querySelectorAll(".cell:last-child .dataText")).forEach(function(cell) {
  if (cell.textContent == "Open")
    cell.parentNode.parentNode.style.backgroundColor = "green";
  else if (cell.textContent == "Closed")
    cell.style.color = "red";
});
&#13;
#table {
  display: table;
}

.row {
  display: table-row;
}

.cell {
  display: table-cell;
  padding: 15px;
}
&#13;
<div id="table">
  <div class="row">
    <div class="cell">
      <div class="dataText">a</div>
    </div>
    <div class="cell">
      <div class="dataText">b</div>
    </div>
    <div class="cell">
      <div class="dataText">c</div>
    </div>
    <div class="cell">
      <div class="dataText">Open</div>
    </div>
  </div>
  <div class="row">
    <div class="cell">
      <div class="dataText">1</div>
    </div>
    <div class="cell">
      <div class="dataText">2</div>
    </div>
    <div class="cell">
      <div class="dataText">3</div>
    </div>
    <div class="cell">
      <div class="dataText">Closed</div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

</body>之前:

<script>
  Array.prototype.slice.call(document.querySelectorAll('.cell:last-of-type'))
  .forEach(function (cell) {
    cell.parentElement.className = 'row ' + (cell.innerText === 'Open' ? 'open' : 'closed');
  });
</script>

并附加到CSS:

.row.open {
  background: green;
}
.row.closed {
  color: red;
}

答案 3 :(得分:0)

试试这个:

<span style="color:green">open</span>