亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有辦法在javascript中將表更改為多維數組?

有沒有辦法在javascript中將表更改為多維數組?

泛舟湖上清波郎朗 2023-07-20 10:24:09
我制作了一個具有行和列屬性的表格,代碼如下:<table border='2px' cellpadding='20px'>  <tr id='rowZero'>      <td id='zeroZero'>-</td>      <td id='zeroOne'>-</td>      <td id='zeroTwo'>-</td>      <td id='zeroThree'>-</td>      <td id='zeroFour'>-</td>``  </tr>  <tr id='rowOne'>       <td id='oneZero'>-</td>      <td id='oneOne'>-</td>      <td id='oneTwo'>-</td>      <td id='oneThree'>-</td>      <td id='oneFour'>-</td>  </tr>但我不知道如何將其轉換為多維數組。我想用這種模式訪問我的表格的“innerText”:td id='zeroZero' on array index [0,0]td id='zeroOne' on array index [0,1]td id='zeroTwo' on array index [0,2]td id='zeroThree' on array index [0,3]td id='zeroFour' on array index [0,4]td id='oneZero' on array index [1,0]td id='oneOne' on array index [1,1]td id='oneTwo' on array index [1,2]td id='oneThree' on array index [1,3]td id='oneFour' on array index [1,4]誰能給我用這種模式將表轉換為數組的代碼?
查看完整描述

1 回答

?
慕容708150

TA貢獻1831條經驗 獲得超4個贊

function tableToArray(table) {

  return [...table.querySelectorAll('tr')].map(row => {

    return [...row.querySelectorAll('td')].map(cell => cell.textContent)

  })

}

你可以使用它傳遞你的表作為參數


const table = document.querySelector('table')

const array = tableToArray(table)


查看完整回答
反對 回復 2023-07-20
  • 1 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號