在JavaScript函数中引用表中的左侧单元格

use*_*046 1 javascript jquery

我有一个js函数需要根据第5个中的值格式化表中的第6列.我使用了以下但它不起作用:

$('tr td:nth-child(6)').each(
    function() {
      var vGood = 0.2
          good = -0.5,
          avg = 0,
          poor = 0.5,
          vPoor = -0.2
      score= $('tr td:nth-child(5)').text(); 
      if (score >= vGood) {
          $(this).addClass('fa fa-thumbs-up fa-lg text-danger latest-icon');          
      }
})
Run Code Online (Sandbox Code Playgroud)

Mam*_*mun 5

您必须使用在每次迭代中this引用正确的td.此外,由于您parseFloat()要将文本与浮点值进行比较,因此请使用将默认字符串值转换td为float.

更改

score= $('tr td:nth-child(5)').text(); 
Run Code Online (Sandbox Code Playgroud)

score = parseFloat($(this).prev('td').text()); 
Run Code Online (Sandbox Code Playgroud)