這是對表格進行排序的一個jquery,誰能解釋一下這個函數頭啊?如下:{ var $sortOrder = 0; //排序類型 1表示升序,0表示降序var $table = $('table#shop');$('th', $table).each(function( column ){//處理三種有可能存在的排序字段,比較方法var findSortKey;if( $(this).is('.sort-title') || $(this).is('.sort-author') ){findSortKey = function( $cell ){return $cell.find('.sort-title').text().toUpperCase()+ '' +$cell.text().toUpperCase();}}else if( $(this).is('.sort-date') ){findSortKey = function( $cell ){return Date.parse('1' + $cell.text());}}else if( $(this).is('.sort-price') ){findSortKey = function( $cell ){var key = parseFloat($cell.text().replace(/^[^\d.]*/, ''))return isNaN(key) ? 0 : key;}}//排序if( findSortKey ){$(this).addClass('clickable').hover(function(){$(this).addClass('hover');var $title = $sortOrder == 0 ? '升序' : '降序';$(this).attr('title', '按'+ $(this).html() + $title +'排列');}, function(){$(this).removeClass('hover');}).click(function(){$sortOrder = $sortOrder == 0 ? 1 : 0;var rows = $table.find('tbody > tr').get();$.each( rows, function( index, row ){row.sortKey = findSortKey($(row).children('td').eq(column));});//排序方法rows.sort(function( a, b ){if( $sortOrder == 1 ){//升序if(a.sortKey < b.sortKey) return -1;if(a.sortKey > b.sortKey) return 1; return 0;}else{//降序if(a.sortKey < b.sortKey) return 1;if(a.sortKey > b.sortKey) return -1; return 0;} });//排序后的對象添加給$table$.each( rows, function( index, row ){$table.children('tbody').append(row);row.sortKey = null;});$table.find('td').removeClass('sorted').filter(':nth-child('+ (column + 1) +')').addClass('sorted');//重新賦予奇偶行的樣式$table.alterRowColors();});} }); });
2 回答

qq_花開花謝_0
TA貢獻1835條經驗 獲得超7個贊
$(document).ready(function() -->載入時執行function()
// $('table.sortable').each(function() -->對每個class="sortble"的table執行function()
// var $table = $(this); -->創建一個jquery對象的$table,并分別用$(this)也就是上面的每個class="sortble"的table給 這個$table賦值

搖曳的薔薇
TA貢獻1793條經驗 獲得超6個贊
$('th','table')兩者是并列的,選擇所有th,table標簽
$('table th')是從屬關系,選擇所有table下的th標簽,不包含table標簽。
經過測試發現:
1.$('th','table') 確實是api文檔所說的 'table'為原始DOM節點 ,所以返回的為th的所有元素
2.$('th , table')兩者是并列的,選擇所有th,table標簽
3.$('table th')是從屬關系,選擇所有table下的th標簽,不包含table標簽。
那我認為 $('th','table') 與 $('table th') 效果是一樣的
添加回答
舉報
0/150
提交
取消