Jquery-获取表中第一个td的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2931234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery- Get the value of first td in table
提问by FlyingCat
I am trying to get the value of first td in each tr when a users clicks "click".
当用户单击“单击”时,我试图获取每个 tr 中第一个 td 的值。
The result below will output aa ,ee or ii. I was thinking about using closest('tr').. but it always output "Object object". Not sure what to do on this one.
下面的结果将输出 aa 、ee 或 ii。我正在考虑使用最接近的('tr')..但它总是输出“对象对象”。不知道该怎么办。
My html is
我的 html 是
<table>
<tr>
<td>aa</td>
<td>bb</td>
<td>cc</td>
<td>dd</td>
<td><a href="#" class="hit">click</a></td>
</tr>
<tr>
<td>ee</td>
<td>ff</td>
<td>gg</td>
<td>hh</td>
<td><a href="#" class="hit">click</a></td>
</tr>
<tr>
<td>ii</td>
<td>jj</td>
<td>kk</td>
<td>ll</td>
<td><a href="#" class="hit">click</a></td>
</tr>
</table>
Jquery
查询
$(".hit").click(function(){
var value=$(this).// not sure what to do here
alert(value) ;
});
回答by Tesserex
$(this).parent().siblings(":first").text()
parent
gives you the <td>
around the link,
parent
给你<td>
周围的链接,
siblings
gives all the <td>
tags in that <tr>
,
siblings
给出了所有的<td>
标签<tr>
,
:first
gives the first matched element in the set.
:first
给出集合中第一个匹配的元素。
text()
gives the contents of the tag.
text()
给出标签的内容。
回答by Felix Kling
This should work:
这应该有效:
$(".hit").click(function(){
var value=$(this).closest('tr').children('td:first').text();
alert(value);
});
Explanation:
解释:
.closest('tr')
gets the nearestancestor that is a<tr>
element (so in this case the row where the<a>
element is in)..children('td:first')
gets all the children of this element, but with the:first
selector we reduce it to the first<td>
element..text()
gets the text inside the element
.closest('tr')
获取作为元素的最近祖先<tr>
(因此在这种情况下是<a>
元素所在的行)。.children('td:first')
获取此元素的所有子元素,但使用:first
选择器我们将其减少到第一个<td>
元素。.text()
获取元素内的文本
As you can see from the other answers, there is more than only one way to do this.
正如您从其他答案中看到的那样,有不止一种方法可以做到这一点。
回答by Inaimathi
In the specific case above, you could do parent/child juggling.
在上面的特定情况下,您可以进行父母/孩子的杂耍。
$(this).parents("tr").children("td:first").text()
回答by Caleb
$(".hit").click(function(){
var values = [];
var table = $(this).closest("table");
table.find("tr").each(function() {
values.push($(this).find("td:first").html());
});
alert(values);
});
You should avoid $(".hit")
it's really inefficient. Try using event delegation instead.
你应该避免$(".hit")
它真的效率低下。尝试改用事件委托。
回答by Marc
Install firebug and use console.log
instead of alert
. Then you will see the exact element your accessing.
安装 firebug 并使用console.log
而不是alert
. 然后您将看到您访问的确切元素。
回答by Shadmehr Vadoodi
If you need to get all td's inside tr without defining id for them, you can use the code below :
如果您需要在 tr 中获取所有 td 而不为它们定义 id,您可以使用以下代码:
var items = new Array();
$('#TABLE_ID td:nth-child(1)').each(function () {
items.push($(this).html());
});
The code above will add all first cells inside the Table into an Array variable.
上面的代码会将 Table 中的所有第一个单元格添加到一个 Array 变量中。
you can change nth-child(1)which means the first cell to any cell number you need.
您可以将nth-child(1)这意味着第一个单元格更改为您需要的任何单元格编号。
hope this code helps you.
希望此代码对您有所帮助。