gpt4 book ai didi

javascript - 根据文本内容动态改变背景颜色

转载 作者:行者123 更新时间:2023-12-01 02:52:09 25 4
gpt4 key购买 nike

我有一列 GPA,我想根据 3.00 到 4.00 的文本内容动态着色。我希望 3.00 有蓝色背景,4.00 有红色背景。

我在这里尝试了 document.querySelectorAll,但它不起作用?

var colorList = document.querySelector('.colorList');

colorList.style.backgroundColor = '#74abe5';

// This is just to demonstrate what color I'd like the highest cell to be.

var colorListEnd = document.querySelector('.colorListEnd');

colorListEnd.style.backgroundColor = '#d7727d'

http://jsbin.com/razicenari/edit?html,js,output

我所拍摄内容的视觉表示:/image/4ICIT.jpg

感谢您的帮助

最佳答案

你可以这样做:

function convertRange( value, r1, r2 ) { 
return ( value - r1[ 0 ] ) * ( r2[ 1 ] - r2[ 0 ] ) / ( r1[ 1 ] - r1[ 0 ] ) + r2[ 0 ];
}

var colorList = document.querySelectorAll('.colorList');

colorList.forEach(function(color){
var colorPicker = convertRange(color.innerText, [3.0, 4.0], [50, 200]);
color.style.backgroundColor = "rgb(" + Math.floor(colorPicker) + ",20," + Math.floor(255 - colorPicker) + ")";
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<table class="table table-striped table-sm">
<thead class="thead-inverse">
<tr>

<th>GPA** <br> 50%
</th>
<th>75%</th>
<th>Max</th>
<th>SAT Reading*** <br>25%-75%</th>
<th>SAT Math*** <br>25%-75%</th>
</tr>
</thead>
<tbody>
<tr>
<td class="colorList">3.52</td>
<td>3.80</td>
<td>4.00</td>
<td>400-510</td>
<td>400-530</td>
</tr>
<tr>
<td class="colorList">3.62</td>
<td>3.80</td>
<td>4.00</td>
<td>400-510</td>
<td>400-530</td>
</tr>
<tr>
<td class="colorList">3.72</td>
<td>3.80</td>
<td>4.00</td>
<td>400-510</td>
<td>400-530</td>
</tr>
<tr>
<td class="colorList">3.82</td>
<td>3.80</td>
<td>4.00</td>
<td>400-510</td>
<td>400-530</td>
</tr>
<tr>
<td class="colorList">3.92</td>
<td>3.80</td>
<td>4.00</td>
<td>400-510</td>
<td>400-530</td>
</tr>
</tbody>
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</table>
</body>
</html>

关于javascript - 根据文本内容动态改变背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46898811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com