1. 程式人生 > >js從table中獲得所需資訊轉為json

js從table中獲得所需資訊轉為json

最近總是在改別人的專案,有些是沒有辦法讀到原始碼的,又要獲得顯示的資訊,這個是原生的js,只需要拷到瀏覽器執行就能獲得當前table中的所需資訊  var txt=document.getElementsByTagName("table"); var table = txt[txt.length-1]; var rowNoColumn; var noColumn; var contractAnnexColumn; var validContractColumn; //確定提取那些列資訊 for (var i = 0; i < table.rows[0].cells.length; i++) { if(!table.rows[0].cells[i].innerText){ rowNoColumn = i; } if("編號" == table.rows[0].cells[i].innerText){ noColumn = i; } if("合同名稱" == table.rows[0].cells[i].innerText){ nameColumn = i; } if("合同附件" == table.rows[0].cells[i].innerText){ contractAnnexColumn = i; } if("生效合同" == table.rows[0].cells[i].innerText){ validContractColumn = i; } } var contractList = []; //遍歷每一行資訊 for (var i = 1; i < table.rows.length; i++) { var contract=new Object(); var row = table.rows[i]; //獲得格子中的文字資訊 contract.rowNo = row.cells[rowNoColumn].innerText; contract.no = row.cells[noColumn].innerText; contract.name = row.cells[nameColumn].innerText; //合同附件 獲得格子中的html資訊 var contractAnnexList = table.rows[i].cells[contractAnnexColumn]; contractAnnexList = contractAnnexList.innerHTML; //獲取html中更加詳細的資訊 var aa = contractAnnexList.split('<a href="###"'); var contractAnnexs = []; for (var j = 1; j < aa.length; j++) { if(aa[j].indexOf("(")){ var bb = aa[j].substring(aa[j].indexOf("(")+1,aa[j].indexOf(")")); contractAnnexs.push(bb); } } contract.contractAnnexs = contractAnnexs; //生效合同 var validContractList = table.rows[i].cells[validContractColumn]; validContractList = validContractList.innerHTML; aa = validContractList.split('<a href="###"'); var validContracts = []; for (var j = 1; j < aa.length; j++) { if(aa[j].indexOf("(")){ var bb = aa[j].substring(aa[j].indexOf("(")+1,aa[j].indexOf(")")); validContracts.push(bb); } } contract.validContracts = validContracts; contractList.push(contract); // 添 }; console.log(JSON.stringify(contractList) );