【发布时间】:2018-10-07 20:45:19
【问题描述】:
第三次尝试。我有一个从远程服务器拉出的表,我使用$('tr:nth-child(row#)').hide(); 隐藏了一些行。现在表格行颜色不会交替。此外,表格单元格会根据单元格中的内容更改颜色。如果单元格值为 81-100%,则为绿色,在 61-80% 时为橙色,在 0-60% 时为红色。这是我的jQuery:
$(document).ready(function(){
$('tr:even').addClass('even'); $('tr:odd').addClass('odd');
});
var $trs = $('tr').removeClass('even odd').filter(":visible");
$trs.filter(':even').addClass('even');
$trs.filter(':odd').addClass('odd');
这是我的 CSS:tr.odd td{ background-color: #FFFFFF;} tr.even td{background-color: #C0C0C0;}
上面的代码实际上是交替的行,但是它删除了单元格的颜色。而tr.odd{ background-color: #FFFFFF;} tr.even{background-color: #C0C0C0;} 保留了单元格的颜色,但删除了交替的颜色。请帮忙。
这是我的整个 HTML:
<html>
<head>
<link type="text/css" rel="stylesheet" href="css.css" />
<script src="jquery-1.10.1.min.js"></script>
<script language="Javascript">
function View(){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("datatable").innerHTML=xmlhttp.responseText;
$("td").filter(function(){ return $(this).text()=='R1';}).text('Row1'); //white
$("td").filter(function(){ return $(this).text()=='R2';}).text('Row2'); //grey
$('tr:nth-child(3)').hide(); //white
$("td").filter(function(){ return $(this).text()=='R4';}).text('Row4'); //grey
$('tr:nth-child(5)').hide(); //white
$("td").filter(function(){ return $(this).text()=='R6';}).text('Row6'); //grey
$("td").filter(function(){ return $(this).text()=='R7';}).text('Row7'); //white
// Alternate visible rows to white and grey
$(document).ready(function(){
$('tr:even').addClass('even');
$('tr:odd').addClass('odd');
});
var $trs = $('tr').removeClass('even odd').filter(":visible");
$trs.filter(':even').addClass('even');
$trs.filter(':odd').addClass('odd');
}
}
var parameters = "search="+"rog_en_vo_ts_t1";
xmlhttp.open("POST", "http://process_this_table.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(parameters);
}
</script>
</head>
<body onload="View();" >
<div id="datatable" align="center"></div>
</body>
</html>
这是我的 CSS:
tr.odd{background-color: #FFFFFF;}
tr.even{background-color: #C0C0C0;}
请帮忙。
【问题讨论】:
-
你能提供一个jsFiddle吗?
-
这里是 jsfiddle.net/nTtyd。
-
你的小提琴不起作用,你的 ajax 调用也是一个本地地址 - 所以我们看不到你拉回的数据
-
您好 user887515。不知道如何使用小提琴。请询问与我的代码相关的任何内容,我会解释。谢谢。
-
我也不确定如何模拟 ajax 调用,所以我们将跳过它。你是如何根据单元格值设置颜色的,我在小提琴中看不到。
标签: jquery html-table