简体   繁体   English

在同一表格单元格中更改文本和图标的颜色

[英]Changing color for text and icon in same table cell

I know how to set a different color for each text value. 我知道如何为每个文本值设置不同的颜色。

First, I'm checking and setting $driverStatus as a string: 首先,我正在检查$ driverStatus并将其设置为字符串:

if ($log->field_is_online_value == 'online') {
    $driverStatus = 'Online';
} elseif ($log->field_is_online_value == 'offline') {
    $driverStatus = 'Offline';
} else {
    $driverStatus = 'Busy';
}

Then forming the table and pasting the string from PHP: 然后形成表并从PHP粘贴字符串:

<table id="locations">
    <tr><th>Driver status</th></tr>
    <tr><td>'.$driverStatus.'</td></tr>
</table>

And finally checking text and changing the color respectively: 最后分别检查文本和更改颜色:

var cells = document.getElementById("locations").getElementsByTagName("td");

for (var i = 0; i < cells.length; i++) {
    if (cells[i].innerHTML == "Online") {
        cells[i].style.color = "#5CB85C";
    } else if (cells[i].innerHTML == "Offline") {
        cells[i].style.color = "#D9534F";
    } else if (cells[i].innerHTML == "Busy") {
        cells[i].style.color = "#F0AD4E";
    }
}

I need to set the same color for both text and icon if string looks like this: 如果字符串看起来像这样,我需要为文本和图标设置相同的颜色:

$driverStatus = 'Online&nbsp&nbsp<i class="glyphicon glyphicon-ok"></i>';
$driverStatus = 'Offline&nbsp&nbsp<i class="glyphicon glyphicon-off"></i>';
$driverStatus = 'Busy&nbsp&nbsp<i class="glyphicon glyphicon-time"></i>';
$driverStatus = 'Online&nbsp&nbsp<i class="glyphicon glyphicon-ok"></i>';
//...

var cells = document.getElementById("locations").getElementsByTagName("td");

for (var i = 0; i < cells.length; i++) {
    var cell = cells[i];
    var icon = cell.getElementsByTagName("i")[0];

    if (cell.innerHTML == 'Online&nbsp&nbsp<i class="glyphicon glyphicon-ok"></i>') {
        cell.style.color = "#5CB85C";
        icon.style.color = "#5CB85C";
    } else if (...) {
        ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM