Example sharing of jquery implementation of table sorting function
This article mainly introduces the table sorting function implemented by jquery, involving the dynamic operation of page element attributes and mouse event response related skills based on jQuery. Friends in need can refer to it. I hope it can help everyone.
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>Document</title> </head> <style type="text/css"> p { width: 1024px; margin: 0 auto; /*p相对屏幕左右居中*/ } table { border: solid 1px #666; border-collapse: collapse; width: 100%; cursor: default; /*该属性定义了鼠标指针放在一个元素边界范围内时所用的光标形状,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> <SCRIPT type="text/javascript" src="jquery-1.7.2.min.js"> </script> <script type="text/javascript"> $(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所在的列号 //给表态th增加鼠标位于上方时发生的事件 $(this).mouseover(function () { tbBodyTr.each(function () {//编列tbody下的tr var tds = $(this).find("td"); //获取列号为参数index的td对象集合 $(tds[thisIndex]).addClass("hover");//给列号为参数index的td对象添加样式 }); }).mouseout(function () {//给表头th增加鼠标离开时的事件 tbBodyTr.each(function () { var tds = $(this).find("td"); $(tds[thisIndex]).removeClass("hover");//鼠标离开时移除td对象上的样式 }); }); $(this).click(function () {//给当前表头th增加点击事件 var dataType = $(this).attr("type");//点击时获取当前th的type属性值 checkColumnValue(thisIndex, dataType); }); }); $("tbody tr").removeClass(); //先移除tbody下tr的所有css类 //table中tbody中tr鼠标位于上面时添加颜色,离开时移除颜色 $("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'); //获取行号为index列的某一行的单元格内容与该单元格所在行的行内容添加到数组trsValue中 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++) { //split() 方法用于把一个字符串分割成字符串数组 //获取每行分割后数组的第一个值,即此列的数组类型,定义了字符串\数字\Ip 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; } }) </script> <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>
The running effect is as follows:
js implementation table sorting_html/css_WEB-ITnose
A practical Javascript class library that implements table sorting_js object-oriented
To achieve super user experience table sorting javascript implementation code_javascript skills
The above is the detailed content of Example sharing of jquery implementation of table sorting function. 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



There will be many AI creation functions in the Doubao app, so what functions does the Doubao app have? Users can use this software to create paintings, chat with AI, generate articles for users, help everyone search for songs, etc. This function introduction of the Doubao app can tell you the specific operation method. The specific content is below, so take a look! What functions does the Doubao app have? Answer: You can draw, chat, write articles, and find songs. Function introduction: 1. Question query: You can use AI to find answers to questions faster, and you can ask any kind of questions. 2. Picture generation: AI can be used to create different pictures for everyone. You only need to tell everyone the general requirements. 3. AI chat: can create an AI that can chat for users,

Both vivox100s and x100 mobile phones are representative models in vivo's mobile phone product line. They respectively represent vivo's high-end technology level in different time periods. Therefore, the two mobile phones have certain differences in design, performance and functions. This article will conduct a detailed comparison between these two mobile phones in terms of performance comparison and function analysis to help consumers better choose the mobile phone that suits them. First, let’s look at the performance comparison between vivox100s and x100. vivox100s is equipped with the latest

With the rapid development of the Internet, the concept of self-media has become deeply rooted in people's hearts. So, what exactly is self-media? What are its main features and functions? Next, we will explore these issues one by one. 1. What exactly is self-media? We-media, as the name suggests, means you are the media. It refers to an information carrier through which individuals or teams can independently create, edit, publish and disseminate content through the Internet platform. Different from traditional media, such as newspapers, television, radio, etc., self-media is more interactive and personalized, allowing everyone to become a producer and disseminator of information. 2. What are the main features and functions of self-media? 1. Low threshold: The rise of self-media has lowered the threshold for entering the media industry. Cumbersome equipment and professional teams are no longer needed.

As Xiaohongshu becomes popular among young people, more and more people are beginning to use this platform to share various aspects of their experiences and life insights. How to effectively manage multiple Xiaohongshu accounts has become a key issue. In this article, we will discuss some of the features of Xiaohongshu account management software and explore how to better manage your Xiaohongshu account. As social media grows, many people find themselves needing to manage multiple social accounts. This is also a challenge for Xiaohongshu users. Some Xiaohongshu account management software can help users manage multiple accounts more easily, including automatic content publishing, scheduled publishing, data analysis and other functions. Through these tools, users can manage their accounts more efficiently and increase their account exposure and attention. In addition, Xiaohongshu account management software has

PHP Tips: Quickly implement the function of returning to the previous page. In web development, we often encounter the need to implement the function of returning to the previous page. Such operations can improve the user experience and make it easier for users to navigate between web pages. In PHP, we can achieve this function through some simple code. This article will introduce how to quickly implement the function of returning to the previous page and provide specific PHP code examples. In PHP, we can use $_SERVER['HTTP_REFERER'] to get the URL of the previous page

"Exploring Discuz: Definition, Functions and Code Examples" With the rapid development of the Internet, community forums have become an important platform for people to obtain information and exchange opinions. Among the many community forum systems, Discuz, as a well-known open source forum software in China, is favored by the majority of website developers and administrators. So, what is Discuz? What functions does it have, and how can it help our website? This article will introduce Discuz in detail and attach specific code examples to help readers learn more about it.

Detailed explanation of the functions and functions of GDM under Linux In the Linux operating system, GDM (GNOMEDisplayManager) is a graphical login manager that provides an interface for users to log in and log out of the system. GDM is usually part of the GNOME desktop environment, but can be used by other desktop environments as well. The role of GDM is not only to provide a login interface, but also includes user session management, screen saver, automatic login and other functions. The functions of GDM mainly include the following aspects:

PHP is a server-side scripting language widely used in web development. Its main function is to generate dynamic web content. When combined with HTML, it can create rich and colorful web pages. PHP is powerful. It can perform various database operations, file operations, form processing and other tasks, providing powerful interactivity and functionality for websites. In the following articles, we will further explore the role and functions of PHP, with detailed code examples. First, let’s take a look at a common use of PHP: dynamic web page generation: P
