打印tabale 中的tr td其中的内容
const rows = document.querySelectorAll('table tbody tr');
// 跳过第一行(索引0),从索引1开始
for (let i = 1; i < rows.length; i++) {
const row = rows[i];
const cells = row.querySelectorAll('td');
console.log(`第 ${i + 1} 行(实际第 ${i} 行):`);
console.log(` 第1列:`, cells[0]?.textContent.trim());
console.log(` 第2列:`, cells[1]?.textContent.trim());
console.log(` 第4列:`, cells[3]?.textContent.trim());
console.log('---');
}
备用 上面的好用,下面的备用
const rows = document.querySelectorAll('table tbody tr');
console.log(`共找到 ${rows.length} 行`);
rows.forEach((row, index) => {
const cells = row.querySelectorAll('td');
// 获取指定列(注意索引从0开始)
const col1 = cells[0]?.textContent.trim() || ''; // 第1个td
const col2 = cells[1]?.textContent.trim() || ''; // 第2个td
const col4 = cells[3]?.textContent.trim() || ''; // 第4个td
console.log(`第 ${index + 1} 行:`);
console.log(` 第1列:`, col1);
console.log(` 第2列:`, col2);
console.log(` 第4列:`, col4);
});