溫馨提示×

jquery獲取table數據的方法有哪些

小億
310
2023-08-09 21:08:48
欄目: 編程語言

JQuery提供了多種方法來獲取table數據,以下是一些常用的方法:

  1. find():使用選擇器選擇表格中的元素,然后使用text()html()方法獲取元素的內容。
var data = [];
$('#tableId').find('tr').each(function() {
var row = [];
$(this).find('td').each(function() {
row.push($(this).text());
});
data.push(row);
});
  1. children():選擇表格中的直接子元素,然后使用text()html()方法獲取元素的內容。
var data = [];
$('#tableId').children('tbody').children('tr').each(function() {
var row = [];
$(this).children('td').each(function() {
row.push($(this).text());
});
data.push(row);
});
  1. map():將表格中的元素映射為一個數組,然后使用text()html()方法獲取元素的內容。
var data = $('#tableId tr').map(function() {
return $(this).find('td').map(function() {
return $(this).text();
}).get();
}).get();
  1. each():遍歷表格中的每個元素,然后使用text()html()方法獲取元素的內容。
var data = [];
$('#tableId tr').each(function() {
var row = [];
$(this).find('td').each(function() {
row.push($(this).text());
});
data.push(row);
});

以上是一些常用的方法,根據實際需求可以選擇適合的方法來獲取table數據。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女