Javascript 如何在For/in循环中解析int,然后添加一个类来更改CSS中的字体颜色?

Javascript 如何在For/in循环中解析int,然后添加一个类来更改CSS中的字体颜色?,javascript,jquery,css,for-loop,parseint,Javascript,Jquery,Css,For Loop,Parseint,我正试图为我合作的曲棍球队创建一个统计表。对于某些类别,我们有一些特定的指标,比如我们想投30次以上的球,但只允许投25次以下的球;我们希望罚3个以下的点球,但平局超过3个,等等。以下是表格,仅输入2场比赛: <body> <header>2019-20 Team Stats</header> <main> <table class="table table-hover"> <thead

我正试图为我合作的曲棍球队创建一个统计表。对于某些类别,我们有一些特定的指标,比如我们想投30次以上的球,但只允许投25次以下的球;我们希望罚3个以下的点球,但平局超过3个,等等。以下是表格,仅输入2场比赛:

<body>
<header>2019-20 Team Stats</header>
<main>
    <table class="table table-hover">
        <thead>
        <tr>
            <th>Game</th>
            <th>Score</th>
            <th id="goal-f">GF</th>
            <th id="goal-a">GA</th>
            <th id="shot-f">Shots For</th>
            <th id="shot-a">Shots Against</th>
            <th>Shot %</th>
            <th id="pen-t">Pen Taken</th>
            <th id="pen-d">Pen Drawn</th>
            <th>PP %</th>
            <th>PK %</th>
            <th id="fo-p">Face-offs</th>
            <th>Hits</th>
            <th id="blk">BS</th>
            <th id="take-a">TA</th>
            <th id="odd-man">OMA</th>
            <th id="eozp">Extended OZP</th>
            <th id="chance-f">CF</th>
            <th id="chance-a">CA</th>
            <th>Chance +/-</th>
            <th id="turn-o">TO</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>10/11/19 at Boston College</td>
            <td>3-5</td> <!--Score-->
            <td>3</td><!--GF-->
            <td>5</td><!--GA-->
            <td>26</td><!--Shots For-->
            <td>28</td><!--Shots Against-->
            <td>.000</td><!--Shot %-->
            <td class="to-num">5</td><!--Pen Taken-->
            <td>4</td><!--Pen Drawn-->
            <td>33%</td><!--FO%-->
            <td>40%</td><!--PP%-->
            <td>100%</td><!--PK%-->
            <td>9</td><!--Hits-->
            <td>11</td><!--BS-->
            <td>7</td><!--TA-->
            <td>10</td><!--OMA-->
            <td>0</td><!--OZP-->
            <td>13</td><!--CF-->
            <td>17</td><!--CA-->
            <td>-4</td><!--C +/1-->
            <td>12</td><!--TO-->
        </tr>
        <tr>
            <td>10/12/19 at Merrimack</td>
            <td>11-6</td> <!--Score-->
            <td>11</td><!--GF-->
            <td>6</td><!--GA-->
            <td>26</td><!--Shots For-->
            <td>22</td><!--Shots Against-->
            <td>.000</td><!--Shot %-->
            <td>3</td><!--Pen Taken-->
            <td>6</td><!--Pen Drawn-->
            <td>64%</td><!--FO%-->
            <td>50%</td><!--PP%-->
            <td>100%</td><!--PK%-->
            <td>9</td><!--Hits-->
            <td>14</td><!--BS-->
            <td>6</td><!--TA-->
            <td>11</td><!--OMA-->
            <td>2</td><!--OZP-->
            <td>22</td><!--CF-->
            <td>14</td><!--CA-->
            <td>+8</td><!--C +/1-->
            <td>18</td><!--TO-->
        </tr>
        </tbody>
    </table>
</main>

2019-20赛季球队统计数据
游戏
分数
女朋友
GA
射击
射击
射击百分比
钢笔
笔画
PP%
PK%
争球
击打
学士学位
助教
奥玛
扩展分区计划大纲图
查阅
加利福尼亚州
机会+/-
到
19年11月10日在波士顿学院
3-5 
3.
5.
26
28
.000
5.
4.
33%
40%
100%
9
11
7.
10
0
13
17
-4
12
19年12月10日在梅里马克
11-6 
11
6.
26
22
.000
3.
6.
64%
50%
100%
9
14
6.
11
2.
22
14
+8
18
本质上,我已经尝试将.to num类从字符串解析为整数,然后如果该整数<或>3,则添加“负”或“正”类,然后将字体颜色从黑色更改为红色或绿色

for (var i = 0; i < $(".to-num").length; i++) {
var outcome = parseInt($(".to-num")[i]);
if (outcome > 3) {
    $(".to-num").addClass("negative");
} else if (outcome < 3) {
    $(".to-num").addClass("positive");
}
for(var i=0;i<$(“.to num”).length;i++){
var-output=parseInt($(“.to-num”)[i]);
如果(结果>3){
$(“.to num”).addClass(“负数”);
}否则如果(结果<3){
$(“.to num”).addClass(“正”);
}
}


这是我的HTML、CSS和JS的代码笔:

您需要将类设置回同一元素上。实际上,每次都在所有元素上设置类。此外,在解析之前,还需要调用innerText或textContent

你可以做:

$(".to-num").each(function() {
  var outcome = parseInt(this.innerText);
if (outcome > 3) {
    this.classList.add("negative");
} else if (outcome < 3) {
    this.classList.add("positive");
}
});
$(“.to num”)。每个(函数(){
var-output=parseInt(this.innerText);
如果(结果>3){
这个.classList.add(“否定”);
}否则如果(结果<3){
这个.classList.add(“肯定”);
}
});

您需要将类设置回同一元素上。实际上,每次都在所有元素上设置类。此外,在解析之前,还需要调用innerText或textContent

你可以做:

$(".to-num").each(function() {
  var outcome = parseInt(this.innerText);
if (outcome > 3) {
    this.classList.add("negative");
} else if (outcome < 3) {
    this.classList.add("positive");
}
});
$(“.to num”)。每个(函数(){
var-output=parseInt(this.innerText);
如果(结果>3){
这个.classList.add(“否定”);
}否则如果(结果<3){
这个.classList.add(“肯定”);
}
});

实现这一点的最简单方法如下:

// select all elements with the class-name of 'to-num', and
// call jQuery's addClass() method on each element of that
// collection:
$('.to-num').addClass(function() {

  // here we return a class-name as a result of this conditional
  // operator; if the parsed-value of the current element's text
  // is greater than 3 we return the 'negative' class-name,
  // otherwise we return 'positive':
  return parseInt($(this).text().trim(), 10) > 3 ? 'negative' : 'positive';
});
$('.to num').addClass(函数(){
返回parseInt($(this.text().trim(),10)>3?'negative':'positive';
});
*,
::之前,
::之后{
框大小:边框框;
保证金:0;
填充:0;
}
桌子{
边界塌陷:塌陷;
}
t车身tr:n个孩子(奇数){
背景色:#dde;
}
th,
运输署{
背景色:透明;
}
td.否定{
颜色:红色;
}
td.阳性{
颜色:绿色;
}

2019-20赛季球队统计数据
游戏
分数
女朋友
GA
射击
射击
射击百分比
钢笔
笔画
PP%
PK%
争球
击打
学士学位
助教
奥玛
扩展分区计划大纲图
查阅
加利福尼亚州
机会+/-
到
10/11/19
波士顿学院
3-5
3.
5.
26
28
.000
5.
4.
33%
40%
100%
9
11
7.
10
0
13
17
-4
12
10/12/19
梅里马克
11-6
11
6.
26
22
.000
3.
6.
64%
50%
100%
9
14
6.
11
2.
22
14
+8
18

实现这一点的最简单方法如下:

// select all elements with the class-name of 'to-num', and
// call jQuery's addClass() method on each element of that
// collection:
$('.to-num').addClass(function() {

  // here we return a class-name as a result of this conditional
  // operator; if the parsed-value of the current element's text
  // is greater than 3 we return the 'negative' class-name,
  // otherwise we return 'positive':
  return parseInt($(this).text().trim(), 10) > 3 ? 'negative' : 'positive';
});
$('.to num').addClass(函数(){
返回parseInt($(this.text().trim(),10)>3?'negative':'positive';
});
*,
::之前,
::之后{
框大小:边框框;
保证金:0;
填充:0;
}
桌子{
边界塌陷:塌陷;
}
t车身tr:n个孩子(奇数){
背景色:#dde;
}
th,
运输署{
背景色:透明;
}
td.否定{
颜色:红色;
}
td.阳性{
颜色:绿色;
}

2019-20赛季球队统计数据
游戏
分数
女朋友
GA
射击
射击
射击百分比
钢笔
笔画
PP%
PK%
争球
击打
学士学位
助教
奥玛
扩展分区计划大纲图
查阅
加利福尼亚州
机会+/-
到
10/11/19
波士顿学院
3-5
3.
5.
26
28
.000
5.
4.
33%
40%
100%
9
11
7.
10
0
13
17
-4