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

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

獲取HTML表格標簽<table><tbody><th><tr><td>之間的所有文本并生成json

獲取HTML表格標簽<table><tbody><th><tr><td>之間的所有文本并生成json

慕標琳琳 2023-10-30 20:23:49
我有下面的 HTML 表,我想獲取標簽之間的數據,這些數據有時是單行,有時是多行。<table>     <tbody>     <tr>        <th>Role</th>        <th>Device Name</th>        <th>IP Address </th>        <th>MAC Address </th>        <th>Registered </th>        <th>Subscribers </th>        <th>Events </th>     </tr>     <tr>        <td>    CM   </td>        <td>    -   </td>        <td>192.168.7.110&nbsp;</td>        <td>506182488323&nbsp;</td>        <td>XYZ        </td>        <td>&nbsp;Shkdsd30ec1        </td>        <td>Events        </td>     </tr>     </tbody></table>我想使用此表生成 JSON,如下面的代碼使用 javascript{  "Role" : "CM",  "Device Name" : "-",  "IP Address" : "192.168.7.110",  "MAC Address" : "506182488323",  "Registered" : "XYZ",  "Subscribers" : "Shkdsd30ec1",  "Events" : "Events"}如果有更多帶有密鑰的標簽,則應該遞增,例如 Role->Role1->Role2 等。
查看完整描述

2 回答

?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

假設您的 HTML 正文中只有此表...


let t = document.getElementsByTagName("table");

let trs = t[0].getElementsByTagName("tr");

let oKeys = [], oVals = [];

let ths = trs[0].getElementsByTagName("th");

let tds = trs[1].getElementsByTagName("td");

ths = Array.from(ths);

tds = Array.from(tds);

ths.map( item => {

  oKeys.push(item.innerText);

  return ;

});

tds.map( item => {

  oVals.push(item.innerText);

  return ;

});

console.log("O keys ", oKeys);

console.log("oVals ", oVals);

let newObj = {};

oKeys.map( (key, i) => {

  let val = oVals[i];

  Object.assign(newObj, {[key] : val })

});

console.log(newObj);

<table id="myTable">

     <tbody>

     <tr>

        <th>Role</th>

        <th>Device Name</th>

        <th>IP Address </th>

        <th>MAC Address </th>

        <th>Registered </th>

        <th>Subscribers </th>

        <th>Events </th>

     </tr>

     <tr>

        <td>

    CM

   </td>

        <td>

    -

   </td>

        <td>192.168.7.110&nbsp;</td>

        <td>506182488323&nbsp;</td>

        <td>XYZ

        </td>

        <td>&nbsp;Shkdsd30ec1

        </td>

        <td>Events

        </td>

     </tr>

     </tbody>

</table>

newObj 保存您想要的數據。您可以在上述邏輯中添加更多內容。



查看完整回答
反對 回復 2023-10-30
?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

使用 jQuery 進行 dom 選擇,這段 JS 代碼應該可以工作



var myRows = [];

var headersText = [];

var $headers = $("th");


// Loop through grabbing everything

var $rows = $("tbody tr").each(function(index) {

? $cells = $(this).find("td");

? myRows[index] = {};


? $cells.each(function(cellIndex) {

? ? // Set the header text

? ? if(headersText[cellIndex] === undefined) {

? ? ? headersText[cellIndex] = $($headers[cellIndex]).text();

? ? }

? ? // Update the row object with the header/cell combo

? ? myRows[index][headersText[cellIndex]] = $(this).text();

? });

});


// Let's put this in the object like you want and convert to JSON (Note: jQuery will also do this for you on the Ajax request)

var myObj = {

? ? "myrows": myRows

};

console.log(myRows);


查看完整回答
反對 回復 2023-10-30
  • 2 回答
  • 0 關注
  • 178 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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