Jquery,在Chrome中工作正常,但在Firefox和Safari中不能


Jquery,working fine in Chrome, but not in Firefox and Safari

一个简单的列表排序函数。该函数按最近日期(数据日期属性(对列表项进行排序。在chrome中它工作正常,但在Firefox和safari中它什么都不做。但火虫中也没有错误:s。

 $( document ).ready(function() {
      $('#November ul li').sort(function(a,b){
         return new Date($(a).data('date')) < new Date($(b).data('date'));
      }).each(function(){
         $('#November ul').prepend(this);
      })
   });

谁能告诉我我做错了什么?谢谢。

得到了解决方案。在排序之前,我需要使用 toArray(( 方法。但是为什么Chrome在没有添加toArray代码的情况下工作,而Firefox和Safari则不然,这让我感到震惊。最终的代码是这样的。

$( document ).ready(function() {
  $('#December ul li').toArray().sort(function(a,b){
     return new Date($(a).data('date')) < new Date($(b).data('date'));
  }).each(function(){
  $('#December ul').prepend(this);
  })
}); 

感谢您的帮助!

相关文章: