javascript 怎么控制表格的背景颜色?

 我来答
ahuinan
2010-09-15 · 超过50用户采纳过TA的回答
知道小有建树答主
回答量:165
采纳率:0%
帮助的人:161万
展开全部
关键的语句:
document.getElementById("table").style.backgroundColor="red";

例子:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>js控制表格背景颜色</title>
</head>

<body>
<table id="table">
<tr>
<td height="66">测试</td>
</tr>

</table>
</body>
</html>
<script language="javascript" type="text/javascript">
window.onload = function(){ document.getElementById("table").style.backgroundColor="red";
}
</script>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
laverju
2010-09-15 · TA获得超过416个赞
知道小有建树答主
回答量:367
采纳率:0%
帮助的人:246万
展开全部
利用Javascript改变css来控制。。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
494491497
2010-09-16
知道答主
回答量:17
采纳率:0%
帮助的人:7.6万
展开全部
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>使用JavaScript控制表格背景颜色</title>
<script type="text/javascript">
function clickButton(num){
var arrayList = document.getElementsByTagName("tr");

if(num==1){//交替变色
var i =0;
while(i<arrayList.length){
var one = arrayList.item(i);
if(i%2==0){
one.style.backgroundColor="red";
}else
{
one.style.backgroundColor="yellow";
}
i=i+1;
}
}else if(num==2){//奇数变色
var i =0;
while(i<arrayList.length){
var one = arrayList.item(i);
if(i%2==0){
arrayList[i].style.backgroundColor="";
}else
{
one.style.backgroundColor="yellow";
}
i=i+1;
}
}else if(num==3){//偶数变色
var i =0;
while(i<arrayList.length){
var one = arrayList.item(i);
if(i%2==0){
one.style.backgroundColor="red";
}else
{
arrayList[i].style.backgroundColor="";
}
i=i+1;
}
}else if(num==4){
return ;
}else{
return ;
}

}
</script>
</head>

<body>
<input type="button" onclick="clickButton(1)" value="交替换色" />
<input type="button" onclick="clickButton(2)" value="奇数换色" />
<input type="button" onclick="clickButton(3)" value="偶数换色" />
<table>
<tr>
<td>第一行</td>
</tr>

<tr>
<td>第二行</td>
</tr>
<tr>
<td>第三行</td>
</tr>
<tr>
<td>第四行</td>
</tr>
<tr>
<td>第五行</td>
</tr>
</table>
</body>
</html>
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式