偶数行がグレーに着色されたこのテーブルがあります(奇数行は白です):
$("td").filter(function(){ return $(this).text() == '1'; }).text('One');
$("td").filter(function(){ return $(this).text() == '2'; }).text('Two');
$("td").filter(function(){ return $(this).text() == '3'; }).text('Three');
$("td").filter(function(){ return $(this).text() == '4'; }).text('Four');
$("td").filter(function(){ return $(this).text() == '5'; }).text('Five');
$("td").filter(function(){ return $(this).text() == '6'; }).text('Six');
次に、次のように行 3 を削除しました。
$("tr:contains('3')").hide();
これで、テーブルの行 2 と行 4 が一緒に灰色になります。行を削除しても行の色を交互に保持するにはどうすればよいですか?
ここに私のHTMLコードがあります:
<html>
<head>
<link type="text/css" rel="stylesheet" href="css.css" />
<script src="jquery-1.10.1.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() == '1'; }).text('One');
$("td").filter(function(){ return $(this).text() == '2'; }).text('Two');
$("td").filter(function(){ return $(this).text() == '3'; }).text('Three');
$("td").filter(function(){ return $(this).text() == '4'; }).text('Four');
$("td").filter(function(){ return $(this).text() == '5'; }).text('Five');
$("td").filter(function(){ return $(this).text() == '6'; }).text('Six');
$("tr:contains('3')").hide();
}
}
xmlhttp.open("POST", "process_this_table.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send();
}
</script>
</head>
<body onload="View();">
<div id="datatable"></div>
</body>
</html>
そして、ここに私のCSSがあります:
body{
padding: 100px;
margin: 20;
font: 2em Times New Roman, Helvetica, sans-serif;
}
table{
border-collapse: separate;
border-radius: 10px 10px 10px 10px;
border: 1px solid red;
width: 600px;
box-shadow: -20px 20px 20px #313030;
}
td {
padding: 0.4em;
border: 1px solid red;
}
tr の属性を追加するように言われ、提供したコードを正しく挿入するように言われると思いますか? もしそうなら、構文を教えてください。jquery を css に挿入できないことはわかっています。
火曜日 2013 年 6 月 4 日午後 7 時 50 分 EST これは、process_this_table.php 内のテーブル構造です。
<?php
echo "<table>";
$rows=array('row 1','row 2','row 3');
$columns=array('column 1','column 2','column 3');
$rlength=count($rows);
$clength=count($columns);
for($i=0;$i<$rlength;$i++){
echo "<tr>";
if($i%2){
echo "<td style=\"background-color: rgb(212,212,212);\">".$rows[$i]."</td>";
for($j=1;$j<$clength;$j++)
echo "<td style=\"background-color: rgb(212,212,212);>blah blah</td>";
}
else{
echo "<td>".$rows[$i]."</td>";
for($j=1;$j<$clength;$j++)
echo "<td>blah blah</td>";
}
echo "</tr>";
}
echo "</table>";
?>
この process_this_table.php はリモート サーバーからのものであるため、偶数行の交互の灰色 (212,212,212) を変更できません。私の目的では、この $("tr:nth-child(2)").css("background-color", "lightblue") のように、奇数または偶数を行うのではなく、色を変更する行を指定することをお勧めします;. $("tr:nth-child(1)").css("background-color", "lightblue"); のような奇妙な行を指定するのに、そのコードが機能しないのはなぜですか? しますか?
結婚した。2013 年 6 月 5 日午後 12 時 50 分 (東部標準時) すべて順調です。皆さん、特に Karl-Andre のおかげです。とはいえ、もう一つ欲しいことがあります。たとえば、行 4 が行 1 になるように行を再配置するにはどうすればよいですか? 前もって感謝します。