gpt4 book ai didi

jquery - 如何使用 jQuery 获取特定表行中的项目

转载 作者:行者123 更新时间:2023-11-28 05:10:20 25 4
gpt4 key购买 nike

我有一个表格,我想使用 jquery 获取第三行的图像源。

<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Avatar</th>
</tr>
<tr>
<td>Peter</td>
<td>Jason</td>
<td>29</td>
<td><img src="json.gif"/></td>
</tr>
<tr>
<td>Victoria</td>
<td>Cooper</td>
<td>38</td>
<td><img src="cooper.gif"/></td>
</tr>

最佳答案

有几种方法,但是 $('#someTableId').find('tr').eq(2) 会起作用。

find()

eq()

注意

  • 在您的标题行中,您的 HTML 格式不正确,您的行是 th,其中包含 td,它仍然应该是 tr 元素中包含 th 元素,请参阅下面示例中的修复
  • 下面为表格添加一个 ID,以便仅针对该表格的行,根据需要进行调整

这是一个简单的例子

$('#someTableId').find('tr').eq(2).addClass('test');
.test td {
background-color:yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="someTableId">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Avatar</th>
</tr>
<tr>
<td>Peter</td>
<td>Jason</td>
<td>29</td>
<td><img src="json.gif"/></td>
</tr>
<tr>
<td>Victoria</td>
<td>Cooper</td>
<td>38</td>
<td><img src="cooper.gif"/></td>
</tr>
</table>

关于jquery - 如何使用 jQuery 获取特定表行中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58049255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com