在Jquery中遇到排序方法的问题。在chrome上,div正确排序,元素在页面上移动。在Firefox上,元素根本不移动(但似乎在页面上刷新)IE,元素完全消失。
这是我的代码:
(function( $ ) {
$.sortByDate = function( elements, order ) {
var arr = [];
elements.each(function() {
var obj = {},
$el = $( this ),
time = $el.find( "time" ).text(),
date = new Date( $.trim( time ) ),
timestamp = date.getTime();
obj.html = $el[0].outerHTML;
obj.time = timestamp;
arr.push( obj );
});
var sorted = arr.sort(function( a, b ) {
if( order == "ASC" ) {
return a.time > b.time;
} else {
return b.time > a.time;
}
});
return sorted;
};
$(function() {
var $newer = $( "#newer" ),
$older = $( "#older" ),
$content = $( "#list" ),
$elements = $( ".searchlock" );
$(document).ready(function() {
var elements = $.sortByDate( $elements, "DESC" );
var html = "";
for( var i = 0; i < elements.length; ++i ) {
html += elements[i].html;
}
$content[0].innerHTML = html;
$( this ).addClass( "selected" ).
siblings().
removeClass( "selected" );
return false;
})
$newer.click(function() {
var elements = $.sortByDate( $elements, "DESC" );
var html = "";
for( var i = 0; i < elements.length; ++i ) {
html += elements[i].html;
}
$content[0].innerHTML = html;
$( this ).addClass( "selected" ).
siblings().
removeClass( "selected" );
return false;
});
$older.click(function() {
var elements = $.sortByDate( $elements, "ASC" );
var html = "";
for( var i = 0; i < elements.length; ++i ) {
html += elements[i].html;
}
$content[0].innerHTML = html;
$( this ).addClass( "selected" ).
siblings().
removeClass( "selected" );
return false;
});
});
})( jQuery );
我尝试过的事情: 在函数之外移动var arr,使其成为全局:不起作用
这是我在codio上的测试网站: http://jargon-cool.codio.io:3000/
感谢您的帮助!