我想给所有(包括<th>
)和最后一列的一些具体类别FIRST细胞,我尝试这样做:选择表列(jQuery的)
$("table tr:first-child td:first-child").addClass("first-col-cell");
$("table tr:last-child td:last-child").addClass("last-col-cell");
..但似乎工作不。
希望得到任何帮助。
我想给所有(包括<th>
)和最后一列的一些具体类别FIRST细胞,我尝试这样做:选择表列(jQuery的)
$("table tr:first-child td:first-child").addClass("first-col-cell");
$("table tr:last-child td:last-child").addClass("last-col-cell");
..但似乎工作不。
希望得到任何帮助。
编辑:你的选择是相当接近:
<script type="text/javascript">
$(function() {
$("table tr td:first-child").text('hello');
$("table tr td:last-child").text('goodbye');
});
</script>
<table>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
试着将它分解了一下,你正在做的穿越是不正确
// This will take the first child which is a TD, within any TR in the table.
$("table tr td:first-child").addClass("first-col-cell");
// This will take the first child which is a TR within the table.
$("table tr:first-child").addClass("first-col-cell");
// Just like the above, except now we're doing the last occurances
$("table tr td:last-child").addClass("last-col-cell");
$("table tr:last-child").addClass("last-col-cell");
然后,我们只需要确保该商标达都好
<table>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>3</td>
<td>3</td>
<td>3</td>
</tr>
然后jQuery的应该通过每一个结果如下
<table>
<tr class="first-col-cell">
<td class="first-col-cell">1</td>
<td>1</td>
<td>1</td>
<td class="last-col-cell">1</td>
</tr>
<tr>
<td class="first-col-cell">2</td>
<td>2</td>
<td>2</td>
<td class="last-col-cell">2</td>
</tr>
<tr class="last-col-cell">
<td class="first-col-cell">3</td>
<td>3</td>
<td>3</td>
<td class="last-col-cell">3</td>
</tr>
</table>
Karmi的代码更加紧凑,但无论如何感谢:-) – 3zzy
嗯,我以为你也试图将代码应用到TR标签,至少从你的jQuery代码片段来看这种方式;) – jakeisonline
如果th
元素事......不知道你怎么想要么治疗colspans。
<!doctype html>
<table>
<tr>
<th></th>
<td>blah</td>
<td></td>
</tr>
<tr>
<td colspan="2"></td>
<td></td>
</tr>
<tr>
<td></td>
<td>blah</td>
<td></td>
</tr>
<tr>
<td></td>
<td>blah</td>
<td></td>
</tr>
</table>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("table tr :first-child").text('first').css('background', 'yellow');
$("table tr :last-child").text('last').css('background', 'brown');
});
</script>
我想什么会选择,所有行的第一个和最后一个?我认为这不是OP想要的。 – NawaMan
不,还是一样的。仅选择找到的第一个单元格,我想将这些类应用于列中的所有单元格。谢谢。 – 3zzy
@Nimbuz - 请参阅我的编辑。 – karim79