JS traverses to acquire data within the table (must see)

This article describes how to use JS to get the content of a cell in the form, the table consists of HTML tags such as Table, Tr, TD. Table represents the table, TR represents line, TD represents the row.

1, the general table structure is as follows

 ID   Name  First need to add ID to the table, which is convenient to locate which table , For example 
WING
2, all content methods in the traverses
acquires the table line number
/ ** * Get the table line number * @Param Int ID * @Return Int * / function getTableRowsLength (ID) {var mytable = document.getlementByid (ID); return myTable.Rows.Length;}

acquire a table Number
   / ** * Get a list of columns * @Param Int ID * @Param int index line * @return INT * / FUNCTION GETTABLEROWCELLslength (id, index) {var mytable = document.getlementByid (ID); if (INDEX 
Traversing table content saves to array

/ ** * Traversing table content return array * @Param INT ID table ID * @Return Array * / function gettablecontent (ID) {var mytable = document .getelementByID (ID); var data = []; for (var i = 0, rows = mytable.rows.Length; i

   <mytable.rows.length){
    return mytable.rows[index].cells.length;
  }else{
    return 0;
  }
}
 3 , Traversal table content complete example  

table { Width: 300px; border: 1px solid # 000000; border-collapse: collapse;} td {border: 1px solid # 000000; border-collapse: collapse;}
  / ** * Traversing table content returns an array * @Param Int ID * @Return Array * / Function GetTableContent (ID) {var mytable = document.getlementByID (ID);Var Data = []; for (var i = 0, rows = mytable.rows.Length; i  ';} Document.getlementByid (' result '). InnerHTML = TMP;} <rows; i++){
    for(var j=0,cells=mytable.rows[i].cells.length; j<cells; j++){
      if(!data[i]){
        data[i] = new Array();
      }
      data[i][j] = mytable.rows[i].cells[j].innerHTML;
    }
  }
  return data;
}
  

1
 FDIPZONE     2       <rows; i++){
      for(var j=0,cells=mytable.rows[i].cells.length; j<cells; j++){
        if(!data[i]){
          data[i] = new Array();
        }
        data[i][j] = mytable.rows[i].cells[j].innerHTML;
      }
    }
    return data;
  }

  /** 
   * 显示表格内容
   * @param Int  id 表格id
   */
  function showTableContent(id){
    var data = getTableContent(id);
    var tmp = '';
    for(i=0,rows=data.length; i<rows; i++){
      for(j=0,cells=data[i].length; j<cells; j++){
        tmp += data[i][j] + ',';
      }
      tmp += '
The above JS traversal acquisition forms of data (must see) is the full content of Xiaobian sharing to everyone, I hope I can give you a reference, I hope everyone will support Tumi Cloud.
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment