简体   繁体   English

使用jQuery将整个数据列从HTML表提取到数组中

[英]Extracting entire column of data from HTML table into array using jQuery

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. jQuery是否有一种简单的方法可以从具有id =“table1”的HTML表中获取整个第三列,并为每个数组元素填充一个单元格值的数组。 I am relatively new to jQuery and have not explored its features fully. 我对jQuery相对较新,并没有完全探索它的功能。 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的一些快捷方式令我感到惊讶,所以认为在这里提问可能比将mashing代码拼凑在一起并且看不到任何结果更明智。

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. 这里我正在做的是通过nth-child选择器选择nth-child 3列中的所有单元格。 Then using $.map function to loop through to and use their value to build a array. 然后使用$ .map函数循环并使用它们的值来构建数组。

Working Fiddle 工作小提琴

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/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM