使用jQuery从HTML表中抽取整列数据到数组中

时间:2022-09-25 22:13:29

Could not find a clear and recent explanation of how to achieve this. Does jQuery have a straightforward method for taking the entire third column from a HTML table with id="table1" and populating an array with one cell value per array element. I am relatively new to jQuery and have not explored its features fully. Some of jQuery's shortcuts have amazed me so thought it might be wiser to ask here than to go on mashing code together and seeing no results.

对于如何实现这一目标,目前还没有一个清晰的解释。jQuery是否有一个简单的方法,可以从id="table1"的HTML表中获取整个第三列,并为每个数组元素填充一个单元格值的数组。我对jQuery还比较陌生,还没有对它的特性进行充分的探索。jQuery的一些快捷方式让我感到惊讶,因此我认为,在这里提出这个问题,可能比一起对代码进行mashing并没有结果要明智得多。

2 个解决方案

#1


11  

To build a array out of all elements from 3rd column you can using the following code

要从第3列构建所有元素的数组,可以使用以下代码

var colArray = $('#table1 td:nth-child(3)').map(function(){
       return $(this).text();
   }).get()​;

here What I am doing is selecting all cells in 3rd column by nth-child selector. Then using $.map function to loop through to and use their value to build a array.

这里我要做的是通过第n个子选择器在第3列中选择所有单元格。然后使用美元。映射函数循环到并使用它们的值来构建数组。

Working Fiddle

工作小提琴

#2


2  

You can try this :

你可以试试这个:

var myArray = new Array();
$(document).ready(function() { 
    $("#table1 tr td:nth-child(3)").each(function(i){
       myArray.push($(this).text());
    });
});

Here is an example http://jsfiddle.net/nU6bg/

这里有一个例子http://jsfiddle.net/nU6bg/

#1


11  

To build a array out of all elements from 3rd column you can using the following code

要从第3列构建所有元素的数组,可以使用以下代码

var colArray = $('#table1 td:nth-child(3)').map(function(){
       return $(this).text();
   }).get()​;

here What I am doing is selecting all cells in 3rd column by nth-child selector. Then using $.map function to loop through to and use their value to build a array.

这里我要做的是通过第n个子选择器在第3列中选择所有单元格。然后使用美元。映射函数循环到并使用它们的值来构建数组。

Working Fiddle

工作小提琴

#2


2  

You can try this :

你可以试试这个:

var myArray = new Array();
$(document).ready(function() { 
    $("#table1 tr td:nth-child(3)").each(function(i){
       myArray.push($(this).text());
    });
});

Here is an example http://jsfiddle.net/nU6bg/

这里有一个例子http://jsfiddle.net/nU6bg/