This article summarizes the skills of jQuery to operate Table. Share it for your reference, as follows:
1. Mouse moves the line and changes color
Method 1: Hover(fun(), fun()) method in jQuery, Parameter 1: The first method is to add style function, Parameter 2: The second method is to cancel style function
$("#table1 tr").hover(function(){ $(this).children("td").addClass("hover") },function(){ $(this).children("td").removeClass("hover") })
Method 2:
$("#table1 tr:gt(0)").hover(function() { $(this).children("td").addClass("hover"); }, function() { $(this).children("td").removeClass("hover"); });
2. Different colors of odd and even rows
$("#table1 tbody tr:odd").css("background-color", "#bbf"); $("#table1 tbody tr:even").css("background-color","#ffc"); $("#table1 tbody tr:odd").addClass("odd") $("#table1 tbody tr:even").addClass("even")
3. Hide a line
Copy the codeThe code is as follows:
$("#table1 tbody tr:eq(3)").hide();
4. Hide a column
Method 1:
Copy the codeThe code is as follows:
$("#table1 tr td::nth-child(3)").hide();
Method 2:
Copy the codeThe code is as follows:
$("#table1 tr").each(function(){$("td:eq(3)",this).hide()});
5. Delete a line
//Delete all lines except the first line$("#table1 tr:not(:first)").remove(); //Delete the specified line$("#table1 tr:eq(3)").remove();
6. Delete a column
//Delete all columns except the first column$("#table1 tr th:not(:nth-child(1))").remove(); $("#table1 tr td:not(:nth-child(1))").remove(); //Delete the first column$("#table1 tr td::nth-child(1)").remove();
7. Get (set) the value of a certain cell
//Set the value of the first td of table1 and the second tr.$("#table1 tr:eq(1) td:nth-child(1)").html("value"); //Get the value of the first td of table1 and the second tr.$("#table1 tr:eq(1) td:nth-child(1)").html();
8. Insert a line:
//Insert a line after the second tr$("<tr><td>Insert3</td><td>Insert</td><td>Insert</td><td>Insert</td></tr>").insertAfter($("#table7 tr:eq(1)"));
9. Get the value of the cell specified in each row
var arr = []; $("#table1 tr td:nth-child(1)").each(function (key, value) { ($(this).html()); }); var result = (',');
10. Select all or not
//Method 1://Select all or not select all parameters passed in are event such as: checkAll(event)function checkAll(evt) { evt=evt?evt:; var chall=?:; var tbl=$("#table1"); var trlist=("tr"); for(var i=1;i<;i++) { var tr=$(trlist[i]); var input=("INPUT[type='checkbox']"); ("checked",); } } //Method 2://Select all or not select all parameters passed in are this such as: checkAll(this)function checkAll(evt) { var tbl=$("#table1"); var trlist=("tr"); for(var i=1;i<;i++) { var tr=$(trlist[i]); var input=("INPUT[type='checkbox']"); ("checked",); } } //Method 3://Select all or not select all parameters passed in are this such as: checkAll(this)function checkAll(evt) { $("#table1 tr").find("input[type='checkbox']").each(function(i){ $(this).attr("checked",) }); } //Method 4://Select all or not select all parameters passed in are this such as: checkAll(this)function checkAll(evt) { $("#table1 tr").find("input[type='checkbox']").attr("checked",); }
11. The client dynamically adds and deletes rows
function btnAddRow() { //The line number starts from 0, and the last line is the Add, Delete, and Save button lines, so subtract 2var rownum=$("#table1 tr").length-2; var chk="<input type='checkbox' id='chk_"+rownum+"' name='chk_"+rownum+"'/>"; var text="<input type='text' id='txt_"+rownum+"' name='txt_"+rownum+"' width='75px'/>"; var sel="<select id='sel_"+rownum+"'><option value='1'>male</option><option value='0'>female</option></select>"; var row="<tr><td>"+chk+"</td><td>"+text+"</td><td>"+sel+"</td><td>"+text+"</td><td>"+text+"</td></tr>"; $(row).insertAfter($("#table1 tr:eq("+rownum+")")); } //The client deletes a line//Only one line can be deleted each time, an error occurs when deleting multiple lines.function btnDeleteRow() { $("#table1 tr").find("input[type='checkbox']").each(function(i){ if($(this).attr("checked")) { if(i!=0)//The line title cannot be deleted{ $("#table1 tr:eq("+i+")").remove(); } } }); } //This is better than the above. You can delete multiple records at once.function btnDeleteRow() { $("#table1 tr").each(function(i){ var chk=$(this).find("input[type='checkbox']"); if(("id")!="checkall")//Cannot delete the title line{ if(("checked")) { $(this).remove(); } } }); } //Client savingfunction btnSaveClick() { //In the find() method, I don't know how to set multiple filter conditions for the time being, so I can't get the value of the select list below//$("#table1 tr td").find("input[type='text']" || "select").each(function(i){ //alert($(this).val()); //}); $("#table1 tr").find("td").each(function(i){ if($(this).find("input[type='text']").length>0) { alert($(this).find("input[type='text']").val()); } else if($(this).find("select").length>0) { alert($(this).find("select").val()); } }); }
<style type="text/css"> .hover { background-color:red; } </style> <table border="1" cellpadding="0" cellspacing="0"> <tr> <th> <input type="checkbox" onclick="checkAll1(this)"/> </th> <th>Name</th> <th>gender</th> <th>password</th> <th>address</th> </tr> <tr> <td> <input type="checkbox" /> </td> <td>Zhang San</td> <td>male</td> <td>zhangsan</td> <td>Shanghai</td> </tr> <tr> <td> <input type="checkbox" /> </td> <td>Li Si</td> <td>male</td> <td>lisi</td> <td>Anqing</td> </tr> <tr> <td> <input type="checkbox" /> </td> <td>Wang Wu</td> <td>male</td> <td>beijing</td> <td>Beijing</td> </tr> <tr> <td> <input type="checkbox" /> </td> <td>Anonymous</td> <td>female</td> <td>wumingshi</td> <td>Shanghai</td> </tr> <tr> <td> <input type="checkbox" /> </td> <td>Teacher Zhao</td> <td>male</td> <td>zhaolaoshi</td> <td>Zhejiang</td> </tr> <tr> <td colspan="5" align="center"> <input type="button" runat="server" value="New" onclick="btnAddRow()" /> <input type="button" runat="server" value="delete" onclick="btnDeleteRow()" /> <input type="button" runat="server" value="save" onclick="btnSaveClick()" /> </td> </tr> </table>
I hope this article will be helpful to everyone's jQuery programming.