SoFunction
Updated on 2025-02-28

A summary of jQuery operation table skills

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&lt;;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&lt;;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="&lt;input type='checkbox' id='chk_"+rownum+"' name='chk_"+rownum+"'/&gt;";
var text="&lt;input type='text' id='txt_"+rownum+"' name='txt_"+rownum+"' width='75px'/&gt;";
var sel="&lt;select id='sel_"+rownum+"'&gt;&lt;option value='1'&gt;male&lt;/option&gt;&lt;option value='0'&gt;female&lt;/option&gt;&lt;/select&gt;";
var row="&lt;tr&gt;&lt;td&gt;"+chk+"&lt;/td&gt;&lt;td&gt;"+text+"&lt;/td&gt;&lt;td&gt;"+sel+"&lt;/td&gt;&lt;td&gt;"+text+"&lt;/td&gt;&lt;td&gt;"+text+"&lt;/td&gt;&lt;/tr&gt;";
$(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&gt;0)
{
alert($(this).find("input[type='text']").val());
}
else if($(this).find("select").length&gt;0)
{
alert($(this).find("select").val());
}
});
}

&lt;style type="text/css"&gt;
.hover
{
  background-color:red;
}
&lt;/style&gt;
&lt;table  border="1" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;th&gt;
&lt;input type="checkbox"  onclick="checkAll1(this)"/&gt;
&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;gender&lt;/th&gt;
&lt;th&gt;password&lt;/th&gt;
&lt;th&gt;address&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input type="checkbox"  /&gt;
&lt;/td&gt;
&lt;td&gt;Zhang San&lt;/td&gt;
&lt;td&gt;male&lt;/td&gt;
&lt;td&gt;zhangsan&lt;/td&gt;
&lt;td&gt;Shanghai&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input type="checkbox"  /&gt;
&lt;/td&gt;
&lt;td&gt;Li Si&lt;/td&gt;
&lt;td&gt;male&lt;/td&gt;
&lt;td&gt;lisi&lt;/td&gt;
&lt;td&gt;Anqing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input type="checkbox"  /&gt;
&lt;/td&gt;
&lt;td&gt;Wang Wu&lt;/td&gt;
&lt;td&gt;male&lt;/td&gt;
&lt;td&gt;beijing&lt;/td&gt;
&lt;td&gt;Beijing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input type="checkbox"  /&gt;
&lt;/td&gt;
&lt;td&gt;Anonymous&lt;/td&gt;
&lt;td&gt;female&lt;/td&gt;
&lt;td&gt;wumingshi&lt;/td&gt;
&lt;td&gt;Shanghai&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;input type="checkbox"  /&gt;
&lt;/td&gt;
&lt;td&gt;Teacher Zhao&lt;/td&gt;
&lt;td&gt;male&lt;/td&gt;
&lt;td&gt;zhaolaoshi&lt;/td&gt;
&lt;td&gt;Zhejiang&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="5" align="center"&gt;
&lt;input type="button"  runat="server" value="New" onclick="btnAddRow()" /&gt;
&lt;input type="button"  runat="server" value="delete" onclick="btnDeleteRow()" /&gt;
&lt;input type="button"  runat="server" value="save" onclick="btnSaveClick()" /&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

I hope this article will be helpful to everyone's jQuery programming.