我使用charlietfl对StackOverflow中的StackOverflow问题的回答来对1月至12月的HTML中的月份列进行排序。我只是修改了一些东西,以便我可以在我的网站上使用它。我还使用制表器,以便按字母顺序对其他表列进行排序。问题是它只适用于。其他浏览器不会对月份列进行排序。有时,包装表标题的tr从头标签中跳出来,进入tbody标记。这是我的桌子:http://makzel.com/problems/
是什么导致了这一切?
以下是我的完整jquery代码:
(function($) {
'use strict';
var $window = $(window);
$window.ready(function(){
// Enable sorting for tables
$(".tablesort").tablesorter({
headers: {
0: {
sorter: false
}
}
});
// Sort by month
$('.tablesort th').click(function(){
var sorters =['January','February','March','April','May','June','July','August','September','October','November','December']
var $rows = $('tr:gt(0)').sort(function(a, b){
var aSrcIdx =sorters.indexOf( $(a).find('td:first').text() );
var bSrcIdx = sorters.indexOf( $(b).find('td:first').text());
return aSrcIdx > bSrcIdx;
});
$(this).closest('.tablesort').append($rows);
});
// Tablesort header background change on click
$(".tablesort th").click(function(){
$('.tablesort th').not(this).removeClass('sorted');
$(this).toggleClass('sorted');
});
function postOverflow() {
if ($window.width() < 768) {
$('.tablesort').closest('.post').css("overflow","scroll");
} else{
$('.tablesort').closest('.post').css("overflow","none");
}
}
postOverflow();
$window.resize(postOverflow);
}); // Ready
})(jQuery);下面是它的链接,显示tr标记跳出头标签:test/index.php
发布于 2016-08-01 22:16:52
根据array.sort文档,尝试返回-1将a置于b以下,0使其保持不变,或返回1将b置于a以下。试着替换:
return aSrcIdx > bSrcIdx; 使用
return aSrcIdx < bSrcIdx ? -1 : bSrcIdx < aSrcIdx ? 1 : 0; https://stackoverflow.com/questions/38708828
复制相似问题