jQuery 选择器每行的第一个 td
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16668094/
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 selector first td of each row
提问by Ragen Dazs
I have this stuff:
我有这个东西:
<table>
<tr>
<td>nonono</td> <!-- FIND THIS -->
</td>foobar</td>
</tr>
<tr>
<td>nonono2</td> <!-- FIND THIS -->
</td>foobar2</td>
</tr>
<tr>
<td>nonono3</td> <!-- FIND THIS -->
</td>foobar2</td>
</tr>
</table>
I have tried with $('td:first')
with no luck;
我试过$('td:first')
没有运气;
Expected return: <td>nonono</td>
, <td>nonon2</td>
and <td>nonon3</td>
预期回报:<td>nonono</td>
,<td>nonon2</td>
和<td>nonon3</td>
Thanks in advance!
提前致谢!
回答by James Donnelly
You should use :first-child
instead of :first
:
您应该使用:first-child
代替:first
:
Sounds like you're wanting to iterate through them. You can do this using .each()
.
听起来你想遍历它们。您可以使用.each()
.
Example:
例子:
$('td:first-child').each(function() {
console.log($(this).text());
});
Result:
结果:
nonono
nonono2
nonono3
Alernatively if you're not wanting to iterate:
或者,如果您不想迭代:
$('td:first-child').css('background', '#000');
回答by Mohammad Adil
try this selector -
试试这个选择器 -
$("tr").find("td:first")
Demo -->
http://jsfiddle.net/66HbV/
演示-->
http://jsfiddle.net/66HbV/
Or
或者
$("tr td:first-child")
Demo -->
http://jsfiddle.net/66HbV/1/
演示-->
http://jsfiddle.net/66HbV/1/
</td>foobar</td>
should be <td>foobar</td>
</td>foobar</td>
应该 <td>foobar</td>
回答by Kaleem Nalband
You can do it like this
你可以这样做
$(function(){
$("tr").find("td:eq(0)").css("color","red");
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>col_1</td>
<td>col_2</td>
</tr>
<tr>
<td>col_1</td>
<td>col_2</td>
</tr>
<tr>
<td>col_1</td>
<td>col_2</td>
</tr>
<tr>
<td>col_1</td>
<td>col_2</td>
</tr>
</table>
回答by 97ldave
回答by Parameswaran
var tablefirstcolumn=$("tr").find("td:first")
alert(tablefirstcolumn+"of Each row")
回答by Saad Imran.
try
尝试
var children = null;
$('tr').each(function(){
var td = $(this).children('td:first');
if(children == null)
children = td;
else
children.add(td);
});
// children should now hold all the first td elements
回答by billyonecan
$('td:first-child')
will return a collection of the elements that you want.
$('td:first-child')
将返回您想要的元素的集合。
var text = $('td:first-child').map(function() {
return $(this).html();
}).get();