这个表排序jQuery脚本是如何工作的? jQuery的排序插件 演示
单击标题(水果/数量)。 果 量 葡萄 15 苹果 4. var th=jQuery('th'), li=jQuery('li'), 逆=假; 点击(函数(){ 变量头=$(此), index=header.index(); 标题 .closest('表') .find('td')) .filter(函数(){ 返回$(this).index()==索引; }) .排序(功能(a、b){ a=$(a.text(); b=$(b.text(); 返回( 伊斯南(a)|伊斯南(b)? a>b:+a>+b ) ? 逆?-1:1: 逆?1:-1; },函数(){ 返回此.parentNode; }); 逆=!逆; }); 在上面的程序中,当我单击这个表排序jQuery脚本是如何工作的? jQuery的排序插件 演示,jquery,jquery-ui,jquery-plugins,Jquery,Jquery Ui,Jquery Plugins,单击标题(水果/数量)。 果 量 葡萄 15 苹果 4. var th=jQuery('th'), li=jQuery('li'), 逆=假; 点击(函数(){ 变量头=$(此), index=header.index(); 标题 .closest('表') .find('td')) .filter(函数(){ 返回$(this).index()==索引; }) .排序(功能(a、b){ a=$(a.text(); b=$(b.text(); 返回( 伊斯南(a)|伊斯南(b)? a>b:+a>
时,它会对该列中的行进行排序。它还使用此文件中的.sort
方法
你能解释一下上面的代码是如何工作的,以及它的内部工作原理吗?这是我获得上述代码的链接:
- 注释中的解释:
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Sort plugin for jQuery</title>
</head>
<body>
<h1>Demo</h1>
<p>Click on the headers (fruit/quantity).</p>
<table>
<thead>
<tr>
<th>Fruit</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<tr>
<td>Grape</td>
<td>15</td>
</tr>
<tr>
<td>Apple</td>
<td>4</td>
</tr>
</tbody>
</table>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="jquery.sort.js"></script>
<script>
var th = jQuery('th'),
li = jQuery('li'),
inverse = false;
th.click(function(){
var header = $(this),
index = header.index();
header
.closest('table')
.find('td')
.filter(function(){
return $(this).index() === index;
})
.sort(function(a, b){
a = $(a).text();
b = $(b).text();
return (
isNaN(a) || isNaN(b) ?
a > b : +a > +b
) ?
inverse ? -1 : 1 :
inverse ? 1 : -1;
}, function(){
return this.parentNode;
});
inverse = !inverse;
});
</script>
</body>
</html>
Comaprison(排序)功能:
function compare(a, b) {
return isNaN(a) || isNaN(b) ? a > b : +a > +b;
}
log("isNaN(\"hi\"):" + isNaN("hi"));
log("isNaN(8): " + isNaN(8));
log("isNaN(\"8\"): " + isNaN("8"));
log("compare(\"hi\", \"there\"): " + compare("hi", "there"));
log("compare(\"there\", \"hi\"): " + compare("there", "hi"));
log("compare(2, 4): " + compare(2, 4));
log("compare(4, 2): " + compare(4, 2));
log("compare(\"hi\", 2): " + compare("hi", 2));
比较函数很有趣。为了使其更可读,请考虑以下函数和示例输入/输出:
th.click(function(){
var header = $(this), // get the table header as a JQuery object
index = header.index(); // get the index - column number - of the header
header
.closest('table') // get the table that contains the header
.find('td') // find all the td objects
.filter(function(){
return $(this).index() === index; // but filter that list so that only td's in the relevant column are selected
})
.sort(function(a, b){ // use the external 'sort' function (JQuery plugin) on our list of table data, and sort using this anonymous function to compare
a = $(a).text(); // compare the text content of two td elements, using alphabetic or numeric ordering where appropriate
b = $(b).text();
return (
isNaN(a) || isNaN(b) ?
a > b : +a > +b
) ?
inverse ? -1 : 1 : // and invert the sort order if the 'inverse' flag is true
inverse ? 1 : -1;
}, function(){
return this.parentNode; // the sort function returns a list of sorted elements - so returning the td parent returns the row, which means it sorts the table rows
});
inverse = !inverse; // toggle the inverse flag so that multiple clicks on the header invert the order
});
输出:
function compare(a, b) {
return isNaN(a) || isNaN(b) ? a > b : +a > +b;
}
log("isNaN(\"hi\"):" + isNaN("hi"));
log("isNaN(8): " + isNaN(8));
log("isNaN(\"8\"): " + isNaN("8"));
log("compare(\"hi\", \"there\"): " + compare("hi", "there"));
log("compare(\"there\", \"hi\"): " + compare("there", "hi"));
log("compare(2, 4): " + compare(2, 4));
log("compare(4, 2): " + compare(4, 2));
log("compare(\"hi\", 2): " + compare("hi", 2));
说明:
function compare(a, b) {
return isNaN(a) || isNaN(b) ? a > b : +a > +b;
}
log("isNaN(\"hi\"):" + isNaN("hi"));
log("isNaN(8): " + isNaN(8));
log("isNaN(\"8\"): " + isNaN("8"));
log("compare(\"hi\", \"there\"): " + compare("hi", "there"));
log("compare(\"there\", \"hi\"): " + compare("there", "hi"));
log("compare(2, 4): " + compare(2, 4));
log("compare(4, 2): " + compare(4, 2));
log("compare(\"hi\", 2): " + compare("hi", 2));
如果输入“不是数字”,则isNaN函数返回true,否则返回false。方便的是,它将数字串视为数字。因此,如果我们比较两个数字(无论是否为字符串),我们的比较函数将返回
isNaN("hi"):true
isNaN(8): false
isNaN("8"): false
compare("hi", "there"): false
compare("there", "hi"): true
compare(2, 4): false
compare(4, 2): true
compare("hi", 2): false
+a > +b
添加+
只会将字符串转换为真实的javascript数字对象,这意味着当文本表示数值时,文本不会按字母顺序排序
如果任一单元格的内容不是数字,则比较函数返回
isNaN("hi"):true
isNaN(8): false
isNaN("8"): false
compare("hi", "there"): false
compare("there", "hi"): true
compare(2, 4): false
compare(4, 2): true
compare("hi", 2): false
+a > +b
…它仅为对象应用默认的
运算符。对于字符串,它将按字母顺序对字符串进行排序。注释内联解释:
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Sort plugin for jQuery</title>
</head>
<body>
<h1>Demo</h1>
<p>Click on the headers (fruit/quantity).</p>
<table>
<thead>
<tr>
<th>Fruit</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<tr>
<td>Grape</td>
<td>15</td>
</tr>
<tr>
<td>Apple</td>
<td>4</td>
</tr>
</tbody>
</table>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="jquery.sort.js"></script>
<script>
var th = jQuery('th'),
li = jQuery('li'),
inverse = false;
th.click(function(){
var header = $(this),
index = header.index();
header
.closest('table')
.find('td')
.filter(function(){
return $(this).index() === index;
})
.sort(function(a, b){
a = $(a).text();
b = $(b).text();
return (
isNaN(a) || isNaN(b) ?
a > b : +a > +b
) ?
inverse ? -1 : 1 :
inverse ? 1 : -1;
}, function(){
return this.parentNode;
});
inverse = !inverse;
});
</script>
</body>
</html>
Comaprison(排序)功能:
function compare(a, b) {
return isNaN(a) || isNaN(b) ? a > b : +a > +b;
}
log("isNaN(\"hi\"):" + isNaN("hi"));
log("isNaN(8): " + isNaN(8));
log("isNaN(\"8\"): " + isNaN("8"));
log("compare(\"hi\", \"there\"): " + compare("hi", "there"));
log("compare(\"there\", \"hi\"): " + compare("there", "hi"));
log("compare(2, 4): " + compare(2, 4));
log("compare(4, 2): " + compare(4, 2));
log("compare(\"hi\", 2): " + compare("hi", 2));
比较函数很有趣。为了使其更可读,请考虑以下函数和示例输入/输出:
th.click(function(){
var header = $(this), // get the table header as a JQuery object
index = header.index(); // get the index - column number - of the header
header
.closest('table') // get the table that contains the header
.find('td') // find all the td objects
.filter(function(){
return $(this).index() === index; // but filter that list so that only td's in the relevant column are selected
})
.sort(function(a, b){ // use the external 'sort' function (JQuery plugin) on our list of table data, and sort using this anonymous function to compare
a = $(a).text(); // compare the text content of two td elements, using alphabetic or numeric ordering where appropriate
b = $(b).text();
return (
isNaN(a) || isNaN(b) ?
a > b : +a > +b
) ?
inverse ? -1 : 1 : // and invert the sort order if the 'inverse' flag is true
inverse ? 1 : -1;
}, function(){
return this.parentNode; // the sort function returns a list of sorted elements - so returning the td parent returns the row, which means it sorts the table rows
});
inverse = !inverse; // toggle the inverse flag so that multiple clicks on the header invert the order
});
输出:
function compare(a, b) {
return isNaN(a) || isNaN(b) ? a > b : +a > +b;
}
log("isNaN(\"hi\"):" + isNaN("hi"));
log("isNaN(8): " + isNaN(8));
log("isNaN(\"8\"): " + isNaN("8"));
log("compare(\"hi\", \"there\"): " + compare("hi", "there"));
log("compare(\"there\", \"hi\"): " + compare("there", "hi"));
log("compare(2, 4): " + compare(2, 4));
log("compare(4, 2): " + compare(4, 2));
log("compare(\"hi\", 2): " + compare("hi", 2));
说明:
function compare(a, b) {
return isNaN(a) || isNaN(b) ? a > b : +a > +b;
}
log("isNaN(\"hi\"):" + isNaN("hi"));
log("isNaN(8): " + isNaN(8));
log("isNaN(\"8\"): " + isNaN("8"));
log("compare(\"hi\", \"there\"): " + compare("hi", "there"));
log("compare(\"there\", \"hi\"): " + compare("there", "hi"));
log("compare(2, 4): " + compare(2, 4));
log("compare(4, 2): " + compare(4, 2));
log("compare(\"hi\", 2): " + compare("hi", 2));
如果输入“不是数字”,则isNaN函数返回true,否则返回false。方便的是,它将数字串视为数字。因此,如果我们比较两个数字(无论是否为字符串),我们的比较函数将返回
isNaN("hi"):true
isNaN(8): false
isNaN("8"): false
compare("hi", "there"): false
compare("there", "hi"): true
compare(2, 4): false
compare(4, 2): true
compare("hi", 2): false
+a > +b
添加+
只会将字符串转换为真实的javascript数字对象,这意味着当文本表示数值时,文本不会按字母顺序排序
如果任一单元格的内容不是数字,则比较函数返回
isNaN("hi"):true
isNaN(8): false
isNaN("8"): false
compare("hi", "there"): false
compare("there", "hi"): true
compare(2, 4): false
compare(4, 2): true
compare("hi", 2): false
+a > +b
…它仅为对象应用默认的
运算符。对于字符串,它将按字母顺序对字符串进行排序。谢谢您的评论。这段代码如何返回(isNaN(a)| | isNaN(b)?a>b:+a>+b)Sje397:你能给我解释一下上面程序的sort.jsfile内部代码吗谢谢你的评论。这段代码是如何工作的(isNaN(a)| | isNaN(b)?a>b:+a>+b)Sje397:你能给我解释一下上面程序的sort.jsfile内部代码吗