javascript - 使用 jquery 将类分配给对象中表的所有 td

标签 javascript jquery html css

我正在尝试将给定 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();

FIDDLE

最佳答案

只需使用 jQuery parent > child Selector 即可选择所有 td。

$("#vapEaseSegmentsTable td").addClass("red");

关于javascript - 使用 jquery 将类分配给对象中表的所有 td,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54708181/

相关文章:

javascript - 函数提升中的奇怪行为

javascript - 使用 fetch 获取 API 响应负载

html - CSS DJANGO 和扩展模板

javascript - Rails 应用程序中的隐藏字段未通过 dropzone.js 传递

javascript - 将变量解析为 .get() 函数

javascript - 如何在同一文件中访问 PHP 脚本内的 jQuery 或 javascript 变量?

javascript - 分离输入值

javascript - jQuery .val() 不会生成 'change'

html - 为什么背景不适用于 span 标签?

javascript - 为 float 条形图获取指定格式的数据