日期:2014-05-16  浏览次数:20326 次

表格操作问题,请高手指教.
有没有办法,使一个表格里面所有<td></td> 中,包含 负号('-') 的文字时变成绿色,不包含 负号('-') 全变成红色?

------解决方案--------------------
你在产生数据是判断是否含有-然后更改td的color样式即可。也可单独对含有-的数据两边加font来定义颜色。
------解决方案--------------------
管你多少表格啊?
你的数据是循环动态添加的,
我只要做一个比较让他循环就可以了!
------解决方案--------------------
可以使用jquery方便的实现。请到www.jquery.com上下载jquery.js
<html>
<head>
<title>test.html</title>
<script type="text/javascript" src="jquery.js"></script>
<style>
.red{color:red;}
.green{color:green;})
</style>
<script>
$(function(){
$("td").each(function(){
var value=$(this).text();
if(value.indexOf("-")>0){
$(this).addClass("red");
}else{
$(this).addClass("green");
}
});
}); 
</script>
</head>
<body>&nbsp; 
<table border="1" width="50%">
<tr>
<td> -20.8</td>
<td> 100</td>
<td> -20</td>
</tr>
<tr>
<td> -20.8</td>
<td> 100</td>
<td> 200</td>
</tr>
</talbe>
</body>
</html>
------解决方案--------------------
HTML code
<table width="100%" border="0">
  <tr>
    <td>-asd</td>
    <td>-asdf</td>
    <td>sadf</td>
    <td>asdf</td>
    <td>2112</td>
  </tr>
  <tr>
    <td>121</td>
    <td>123</td>
    <td>-1312</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

<script>
mytd = document.getElementsByTagName("td")
alert(mytd);
for(key=0;key<mytd.length;key++){
v = mytd[key].innerText;
if(/\-/.test(v))
mytd[key].style.color="green"
else
m