javascript 怎么控制表格的背景颜色?
3个回答
展开全部
关键的语句:
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>
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>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
利用Javascript改变css来控制。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<!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>
<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>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询