JavaScript:无法动态更改HTML单元格颜色

时间:2017-05-31 09:37:56

标签: javascript html perl

值" -50.9"和" 10.87"已被分配给下面的html代码作为变量(value1 = -50.9; value2 = 10.87)。

我得到的范围:(无限值至-35),( - 34至-25),( - 24至更大数字)

我认为如果' if条件'是的,单元格背景颜色应该改变吗?

<html>
<head>
<!--<script type="text/javascript">-->
<script language="JavaScript">
    function try() {                                                                                                                                                                                          
    if (-50.9 <= -25|| 10.87 <= -25) {                                                                                                                                                                                 
        document.getElementById("texas").style.backgroundColor='white';                                                                                                                                       
    } else if (-50.9 <= -35 || 10.87 <= -35) {                                                                                                                                                                         
        document.getElementById("texas").style.backgroundColor='blue';                                                                                                                                       
    } else {                                                                                                                                                                                           
        document.getElementById("texas").style.backgroundColor='pink';                                                                                                                                       
    }                                                                                                                                                                                                  
};                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
</script>       
</head>
<body>
 <div id = "div1">
   <table>
     <tr>
       <td id = "texas"><a href="">A = 10.87<br>B = -50.9</a></td>
     </tr>
   </table>
 </div>
</body>
</html>

这能够奏效吗?或者值应该是变量?

修改
我使用perl脚本通过打印内容来生成这个html 并且value1和value2在脚本的开头声明 因此,当脚本打印html表时,它会将变量转换为值  也就是为什么它在JavaScript中是-50.9和10.87。

我想知道if-else条件为真时可以更改单元格颜色 让我们说,在这种情况下,表格单元格应该是蓝色的? 因为目前,表格单元格颜色保持不变,没有任何变化。

1 个答案:

答案 0 :(得分:1)

1.更改您的HTML tr标签并为Value1和Value 2分配单个ID

  function test() {

var value1 = document.getElementById("value1").innerText;

var value2 = document.getElementById("value2").innerText;

value1 = Number(value1);
value2 = Number(value2);

if (value1 <= -25 || value2 <= -25) {
    document.getElementById("texas").style.backgroundColor = 'red';
} else if (value1 <= -35 || value2 <= -35) {
    document.getElementById("texas").style.backgroundColor = 'yellow';
} else {
    document.getElementById("texas").style.backgroundColor = 'green';
}
};

2.然后自定义您的Javascript以实现结果

test();

3.忘记调用函数

public static IEnumerable<string> SplitByLength(this string str, int maxLength) 
  {
    for (int index = 0; index < str.Length; index += maxLength) {
    yield return str.Substring(index, Math.Min(maxLength, str.Length - index));
  }
}
相关问题