Javascript 使用 jquery 读取 HTML 表的第一列值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12752834/
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
Read the first column values of a HTML table with jquery
提问by Milo
I got a table:
我有一张桌子:
<table id="ItemsTable" >?
<tbody>
<tr>
<th>
Number
</th>
<th>
Number2
</th>
</tr>
<tr>
<td>32174711</td> <td>32174714</td>
</tr>
<tr>
<td>32174712</td> <td>32174713</td>
</tr>
</tbody>
</table>
I need the values 32174711 and 32174712 and every other value of the column number into an array or list, i'm using jquery 1.8.2.
我需要将值 32174711 和 32174712 以及列号的所有其他值放入数组或列表中,我使用的是 jquery 1.8.2。
回答by SUN Jiangong
var arr = [];
$("#ItemsTable tr").each(function(){
arr.push($(this).find("td:first").text()); //put elements into array
});
See this link for demo:
有关演示,请参阅此链接:
回答by undefined
You can use map
method:
您可以使用map
方法:
var arr = $('#ItemsTable tr').find('td:first').map(function(){
return $(this).text()
}).get()
From jQuery map()
documentation:
来自 jQuerymap()
文档:
Description: Pass each element in the current matched set through a function, producing a new jQuery object containing the return values. . As the return value is a jQuery-wrapped array, it's very common to get() the returned object to work with a basic array.
描述:通过一个函数传递当前匹配集合中的每个元素,生成一个包含返回值的新 jQuery 对象。. 由于返回值是一个 jQuery 包装的数组,因此 get() 返回的对象与基本数组一起工作是很常见的。
回答by jrummell
// iterate over each row
$("#ItemsTable tbody tr").each(function(i) {
// find the first td in the row
var value = $(this).find("td:first").text();
// display the value in console
console.log(value);
});
回答by Huangism
well from what you have, you can use first-child
从你所拥有的,你可以使用 first-child
var td_content = $('#ItemsTable tr td:first-child').text()
// loop the value into an array or list
回答by Shmiddty
http://jsfiddle.net/Shmiddty/zAChf/
http://jsfiddle.net/Shmiddty/zAChf/
var items = $.map($("#ItemsTable td:first-child"), function(ele){
return $(ele).text();
});
console.log(items);?