-
nannan 数据老手Lv5
发表于2019-5-23 17:09
悬赏1
已解决
楼主
怎么让表格里的这一列数据前2个字显示红色,后面的字显示白色 ?
24个回答
这个是脚本,加在代码的<body>标签中即可
- <script type="text/javascript">
- setTimeout(function(){
- <font color="#ff0000"> //设置表格的某一列的字体颜色,参数分别为表格代号,指定列(从0开始),字体颜色
- setColcolor("grid2",1,"red");</font>
- function setColcolor(grid,col,color){
- 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;
- for (var i = 0; i < rowlen; 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>
nannan 发表于 2019-5-24 14:06
这个代码我是粘过去之后只需要修改表格代号就可以用了吗
//设置表格的某一列的字体颜色,参数分别为表格代号,指定列(从0开始),字体颜色
setColcolor("grid2",1,"red");</font>
缱绻 发表于 2019-5-24 14:12
//设置表格的某一列的字体颜色,参数分别为表格代号,指定列(从0开始),字体颜色
setColcolor("grid2" ...
呃为什么红色还是显示不出来..我是这么写的..分析区表格是grid25
</body>
<script type="text/javascript">
setTimeout(function(){
<font color="#ff0000"> //设置表格的某一列的字体颜色,参数分别为表格代号,指定列(从0开始),字体颜色
setColcolor("grid25",1,"red");</font>
function setColcolor(grid,col,color){
var grid25 = document.getElementById(grid.toUpperCase());
var spans = grid25.getElementsByTagName("span");
var inspan = document.createElement("span");
var collen = grid25.getElementsByTagName("col").length;
var rowlen=grid25.getElementsByTagName("tr").length;
var y=col;
for (var i = 0; i < rowlen; 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;
}
inspan.innerHTML = instr;
}
//将数组拼接成字符串
function addStrAfterInspan(len) {
var str = "";
for (var i = 2; i < len; i++) {
str += arr;
}
return str;
}
}
},400)
</script>