This article mainly introduces three ways to obtain tr td using JavaScript. You can check the detailed explanation below for the specific operation steps. Interested friends can refer to it.
First get the table, then get the tr tag, and then traverse the td */
##
// $('#selectIds').val(""); // var table = document.getElementById("tb_table");//获取第一个表格 // var array = table.getElementsByTagName("tr");//所有tr // for(var i = 1; i < array.length; i++) { // var id = array[i].children; // var idtext = id[1].innerHTML; // if(i == 1){ // $('#selectIds').val($('#selectIds').val() + "" + idtext); // }else{ // $('#selectIds').val($('#selectIds').val() + "," + idtext); // } // }
/* How to write jQuery, get tr and then traverse all td. Note that the value obtained by td is .text(); */
// $('#selectIds').val(""); // var isFirst = true; // $("#tb_table").find("tr").each(function(){ // var tdArr = $(this).children(); // var idtext = tdArr.eq(1).text(); // if(idtext != '人员ID'){ // if(isFirst){ // $('#selectIds').val($('#selectIds').val() + "" + idtext); // isFirst = false // }else{ // $('#selectIds').val($('#selectIds').val() + "," + idtext); // } // console.log("idtext",idtext); // } // })
/* Native js gets all rows, and then gets each cell*/
// var table = document.getElementById("tb_table"); // var rows = table.rows;//获取所有行 // console.log("lenth",rows.length) // // for(var i=1; i < rows.length; i++){ // var row = rows[i];//获取每一行 // var id = row.cells[1].innerHTML;//获取具体单元格 // console.log("id",id) // }
Summary:
$(this).children().eq( 1).text() gets the displayed value $(this).children().eq(1).html() gets the value between