我正在尝试将给定 javascript 对象的类分配给表的所有 td,但我无法实现,这就是我尝试过的方法。我正在遍历所有 tds,在另一个循环中,我正在遍历 json。由于某种原因,我无法将 id 添加到任何元素。
HTML
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table id="vapEaseSegmentsTable">
<thead>
<tr>
<th> </th>
<th>Convert</th>
<th>Maintain</th>
<th>Engage</th>
</tr>
</thead>
<tbody>
<tr>
<th>Email Engaged</th>
<td>0.4%</td>
<td>3.4%</td>
<td>6.4%</td>
</tr>
<tr>
<th>Email Inactive</th>
<td>5.0%</td>
<td>4.4%</td>
<td>7.4%</td>
</tr>
<tr>
<th>Likely to Thrive</th>
<td>8.0%</td>
<td>7.4%</td>
<td>8.4%</td>
</tr>
</tbody>
</table>
</body>
</html>
JQUERY
var easeSegmentNameMap = {
"EMAIL_ENGAGED": {
CONVERT: "highestValue",
MAINTAIN: "mediumValue",
ENGAGE: "lowValue"
},
"EMAIL_INACTIVE": {
CONVERT: "mediumValue",
MAINTAIN: "highestValue",
ENGAGE: "lowValue"
},
"LIKELY_TO_THRIVE": {
CONVERT: "highestValue",
MAINTAIN: "lowValue",
ENGAGE: "mediumValue"
}
}
function setClass() {
$("td").each(function(index, value) {
$.each(easeSegmentNameMap, function(index, easeSegmentName) {
$(this).addClass("add class from easeSegmentNameMap in sequence ");
});
});
}
setClass();
最佳答案
只需使用 jQuery parent > child Selector 即可选择所有 td。
$("#vapEaseSegmentsTable td").addClass("red");
关于javascript - 使用 jquery 将类分配给对象中表的所有 td,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54708181/