-
nannan 数据老手Lv5
发表于2019-5-23 17:09
悬赏1
已解决
楼主
怎么让表格里的这一列数据前2个字显示红色,后面的字显示白色 ?
最佳答案
nannan 发表于 2019-5-24 16:47
好的,太感谢啦
换成这个试试setColcolor("grid2", 1, "red",1);第4个参数是设置从那一行开始字体颜色为红色。
- <script type="text/javascript">
- setTimeout(function() {
- //设置表格的某一列的字体颜色,参数分别为表格代号,指定列(从0开始),字体颜色
- setColcolor("grid2", 1, "red",1);
- function setColcolor(grid, col, color,nonerow) {
- var grid2 = document.getElementById(grid.toUpperCase());
- var spans = grid2.getElementsByTagName("span");
- var inspan = document.createElement("span");
- var collen = grid2.getElementsByTagName("col").length;
- var rowlen = grid2.getElementsByTagName("tr").length;
- var y = col + collen*nonerow;
- for (var i = 0; i < rowlen-nonerow; i++) {
- var str = spans[i + y].innerHTML;
- var arr = str.split('');
- var len1 = arr.length;
- if (len1 !== 0) {
- spans[i + y].appendChild(inspan);
- addStrOfInspan(inspan);
- //设置前两位字颜色
- inspan.style.color = color;
- var reg = new RegExp(".{1," + len1 + "}");
- spans[i + y].innerHTML = spans[i + y].innerHTML.replace(/$/g, addStrAfterInspan(len1)).replace(reg, "");
- } else {
- y = y - 1;
- }
- y = y + collen;
- }
- //添加内部span内容
- function addStrOfInspan(inspan) {
- var instr = "";
- for (var i = 0; i < 2; i++) {
- instr += arr[i];
- }
- inspan.innerHTML = instr;
- }
- //将数组拼接成字符串
- function addStrAfterInspan(len) {
- var str = "";
- for (var i = 2; i < len; i++) {
- str += arr[i];
- }
- return str;
- }
- }
- }, 400)
- </script>
24个回答
您这些数据都是固定的还是从数据库中取出来的呢?