2013-04-08 133 views
3

我有一个按价格ASC和DESC排序div的函数。但它不适用于Safari。 在Firefox/Chrome上无关紧要。jQuery功能在Safari上不起作用

是什么原因?

的代码(和fiddle版):

function sortByPrice(a,b){ 
     return $(a).find('.cicerone_price').text() > $(b).find('.cicerone_price').text(); 
    } 

    function sortByPriceDesc(a,b){ 
    return $(a).find('.cicerone_price').text() < $(b).find('.cicerone_price').text(); 
    } 

    function reorderEl(el){ 
     var container = $('#tabs'); 
     container.html(''); 
     el.each(function(){ 
      $(this).appendTo(container); 
     }); 
    } 
    $('#filter_price').change(function(){ 
     if ($("#filter_price option:selected").val()=='desc'){ 
      reorderEl($('.global_product').sort(sortByPriceDesc)); 
     } else if ($("#filter_price option:selected").val()=='asc'){ 
      reorderEl($('.global_product').sort(sortByPrice)); 
      } 
    }); 
+0

Safari似乎不喜欢你从比较函数中返回一个布尔值进行排序。而是返回-1/1(对于具有相同排序标准的元素,也许为0)。 – CBroe 2013-04-08 14:08:52

+0

你有什么问题吗?我迷路了-_- – artSx 2013-04-08 15:28:51

+0

我更新了你的小提琴 - 在Safari 5.1.7/Win7中以这种方式正常工作。 – CBroe 2013-04-09 08:34:09

回答

2

的问题已经解决,该解决方案,我添加parseFloat十进制

的解决方案是在这里:fiddle correction

function sortByPrice(a,b){ 
    return parseFloat($(a).find('.productPriceForSorting').text()) > parseFloat($(b).find('.productPriceForSorting').text()) ? 1 : -1; 
}