Tor*_*ups 3 html jquery html-table jquery-selectors
我正在尝试在特定类中的tr内的第一个td中添加一个类.但由于某种原因,它只将此类添加到第一个tr中的第一个t - 而不是每个tr中的第一个td
$("#appendTD").find("#gridFormInformation").children(0).children().each(
function() {
if ($("#appendTD").find('tr').filter(function() { return $(this).attr('class') == 'ui-widget-content ui-subtblcell'; }).children("td:eq(0)").addClass("leftbord"));
}
);
Run Code Online (Sandbox Code Playgroud)
但是当我通过删除""td:eq(0)来改变上面的内容时,它会将这个类添加到每个tr中的每个td ......所以我做错了什么?
下面的标记示例
<td id="appendTD">
<table id="gridFormInformation">
<tbody>
<tr><th>1</th><th>2</th><th>3</th></tr>
<tr class="ui-widget-content ui-subtblcell"><td>1</td><td>2</td><td>3</td></tr>
<tr class="ui-widget-content ui-subtblcell"><td>1</td><td>2</td><td>3</td></tr>
<tr class="ui-widget-content ui-subtblcell"><td>1</td><td>2</td><td>3</td></tr>
</tbody>
</table>
</td>
Run Code Online (Sandbox Code Playgroud)
在我看来,您正在使用复杂的手动方式来查找要添加类的tds.我相信使用jQuery选择器来查找你的tds会更容易.
更换
$("#appendTD").find("#gridFormInformation").children(0).children().each(
function() {
if ($("#appendTD").find('tr').filter(function() { return $(this).attr('class') == 'ui-widget-content ui-subtblcell'; }).children("td:eq(0)").addClass("leftbord"));
}
);
Run Code Online (Sandbox Code Playgroud)
同
$("#gridFormInformation tr.ui-widget-content.ui-subtblcell").find("td:first").addClass("leftbord");
Run Code Online (Sandbox Code Playgroud)
这将找到所有trs并为每个tr找到第一个td并将类添加到该td.
| 归档时间: |
|
| 查看次数: |
8078 次 |
| 最近记录: |