Jquery operates table columns and sorts tables
This time I will bring you Jquery to operate table columns and sort tables. What are the precautions for Jquery to operate table columns and sort tables? The following is a practical case, let's take a look.
The main idea of this implementation is: get the column number of the header cell clicked by the mouse, traverse the data rows, get the html in eachIn order to better understand the principle of sorting tables on the front end, and to further learn Jquery, I decided to use Jquery to implement a small function of sorting tables.
The main idea of this implementation is: get the column number of the table header cell clicked by the mouse, traverse the data rows, get the html in each
HTML code list:
View Code <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title> 表格排序 </title> <meta name="Generator" content="EditPlus"> <meta name="Author" content="tschengbin"> <meta name="Keywords" content="jquery tableSort"> <meta name="Description" content=""> <script type="text/javascript" src="jquery-1.8.3.min.js"></script> <script type="text/javascript" src="tableSort.js"></script> <style type="text/css"> p{ width: 1024px; margin: 0 auto;/*p相对屏幕左右居中*/ } table{ border: solid 1px #666; border-collapse: collapse; width: 100%; cursor: default; } tr{ border: solid 1px #666; height: 30px; } table thead tr{ background-color: #ccc; } td{ border: solid 1px #666; } th{ border: solid 1px #666; text-align: center; cursor: pointer; } .sequence{ text-align: center; } .hover{ background-color: #3399FF; } </style> </head> <body> <p> <table id="tableSort"> <thead> <tr> <th type="number">序号</th> <th type="string">书名</th> <th type="number">价格(元)</th> <th type="string">出版时间</th> <th type="number">印刷量(册)</th> <th type="ip">IP</th> </tr> </thead> <tbody> <tr class="hover"> <td class="sequence">1</td> <td>狼图腾</td> <td>29.80</td> <td>2009-10</td> <td>50000</td> <td>192.168.1.125</td> </tr> <tr> <td class="sequence">2</td> <td>孝心不能等待</td> <td>29.80</td> <td>2009-09</td> <td>800</td> <td>192.68.1.125</td> </tr> <tr> <td class="sequence">3</td> <td>藏地密码2</td> <td>28.00</td> <td>2008-10</td> <td></td> <td>192.102.0.12</td> </tr> <tr> <td class="sequence">4</td> <td>藏地密码1</td> <td>24.80</td> <td>2008-10</td> <td></td> <td>215.34.126.10</td> </tr> <tr> <td class="sequence">5</td> <td>设计模式之禅</td> <td>69.00</td> <td>2011-12</td> <td></td> <td>192.168.1.5</td> </tr> <tr> <td class="sequence">6</td> <td>轻量级 Java EE 企业应用实战</td> <td>99.00</td> <td>2012-04</td> <td>5000</td> <td>192.358.1.125</td> </tr> <tr> <td class="sequence">7</td> <td>Java 开发实战经典</td> <td>79.80</td> <td>2012-01</td> <td>2000</td> <td>192.168.1.25</td> </tr> <tr> <td class="sequence" onclick="sortArray()">8</td> <td>Java Web 开发实战经典</td> <td>69.80</td> <td>2011-11</td> <td>2500</td> <td>215.168.54.125</td> </tr> </tbody> </table> </p> </body> </html> tableSort.js代码清单: 复制代码 代码如下: View Code $(document).ready(function(){ var tableObject = $('#tableSort');//获取id为tableSort的table对象 var tbHead = tableObject.children('thead');//获取table对象下的thead var tbHeadTh = tbHead.find('tr th');//获取thead下的tr下的th var tbBody = tableObject.children('tbody');//获取table对象下的tbody var tbBodyTr = tbBody.find('tr');//获取tbody下的tr var sortIndex = -1; tbHeadTh.each(function() {//遍历thead的tr下的th var thisIndex = tbHeadTh.index($(this));//获取th所在的列号 $(this).mouseover(function(){ tbBodyTr.each(function(){//编列tbody下的tr var tds = $(this).find("td");//获取列号为参数index的td对象集合 $(tds[thisIndex]).addClass("hover"); }); }).mouseout(function(){ tbBodyTr.each(function(){ var tds = $(this).find("td"); $(tds[thisIndex]).removeClass("hover"); }); }); $(this).click(function() { var dataType = $(this).attr("type"); checkColumnValue(thisIndex, dataType); }); }); $("tbody tr").removeClass();//先移除tbody下tr的所有css类 $("tbody tr").mouseover(function(){ $(this).addClass("hover"); }).mouseout(function(){ $(this).removeClass("hover"); }); //对表格排序 function checkColumnValue(index, type) { var trsValue = new Array(); tbBodyTr.each(function() { var tds = $(this).find('td'); trsValue.push(type + ".separator" + $(tds[index]).html() + ".separator" + $(this).html()); $(this).html(""); }); var len = trsValue.length; if(index == sortIndex){ trsValue.reverse(); } else { for(var i = 0; i < len; i++){ type = trsValue[i].split(".separator")[0]; for(var j = i + 1; j < len; j++){ value1 = trsValue[i].split(".separator")[1]; value2 = trsValue[j].split(".separator")[1]; if(type == "number"){ value1 = value1 == "" ? 0 : value1; value2 = value2 == "" ? 0 : value2; if(parseFloat(value1) > parseFloat(value2)){ var temp = trsValue[j]; trsValue[j] = trsValue[i]; trsValue[i] = temp; } } else if(type == "ip"){ if(ip2int(value1) > ip2int(value2)){ var temp = trsValue[j]; trsValue[j] = trsValue[i]; trsValue[i] = temp; } } else { if (value1.localeCompare(value2) > 0) {//该方法不兼容谷歌浏览器 var temp = trsValue[j]; trsValue[j] = trsValue[i]; trsValue[i] = temp; } } } } } for(var i = 0; i < len; i++){ $("tbody tr:eq(" + i + ")").html(trsValue[i].split(".separator")[2]); } sortIndex = index; } //IP转成整型 function ip2int(ip){ var num = 0; ip = ip.split("."); num = Number(ip[0]) * 256 * 256 * 256 + Number(ip[1]) * 256 * 256 + Number(ip[2]) * 256 + Number(ip[3]); return num; } })
Detailed explanation of JS JQuery dynamic operation table row method
Detailed explanation of JSON.parse() and JSON. The difference and usage of stringify()
The above is the detailed content of Jquery operates table columns and sorts tables. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



LinuxDeploy operating steps and precautions LinuxDeploy is a powerful tool that can help users quickly deploy various Linux distributions on Android devices, allowing users to experience a complete Linux system on their mobile devices. This article will introduce the operating steps and precautions of LinuxDeploy in detail, and provide specific code examples to help readers better use this tool. Operation steps: Install LinuxDeploy: First, install

With the popularity of smartphones, the screenshot function has become one of the essential skills for daily use of mobile phones. As one of Huawei's flagship mobile phones, Huawei Mate60Pro's screenshot function has naturally attracted much attention from users. Today, we will share the screenshot operation steps of Huawei Mate60Pro mobile phone, so that everyone can take screenshots more conveniently. First of all, Huawei Mate60Pro mobile phone provides a variety of screenshot methods, and you can choose the method that suits you according to your personal habits. The following is a detailed introduction to several commonly used interceptions:

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Ele.me is a software that brings together a variety of different delicacies. You can choose and place an order online. The merchant will make it immediately after receiving the order. Users can bind WeChat through the software. If you want to know the specific operation method , remember to check out the PHP Chinese website. Instructions on how to bind WeChat to Ele.me: 1. First open the Ele.me software. After entering the homepage, we click [My] in the lower right corner; 2. Then in the My page, we need to click [Account] in the upper left corner; 3. Then come to the personal information page where we can bind mobile phones, WeChat, Alipay, and Taobao. Here we click [WeChat]; 4. After the final click, select the WeChat account that needs to be bound in the WeChat authorization page and click Just [Allow];

PHP String Operation: A Practical Method to Effectively Remove Spaces In PHP development, you often encounter situations where you need to remove spaces from a string. Removing spaces can make the string cleaner and facilitate subsequent data processing and display. This article will introduce several effective and practical methods for removing spaces, and attach specific code examples. Method 1: Use the PHP built-in function trim(). The PHP built-in function trim() can remove spaces at both ends of the string (including spaces, tabs, newlines, etc.). It is very convenient and easy to use.

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,
