2014-01-25 3 views
0

jquery에서 정렬을 구현하는 방법. 아래 예제에서 "Var List"는 html 코드를 가지고 있는데, 여기서 "#points"필드를 클릭하면 정렬을 구현해야합니다. 이것은 파이어 폭스에서는 잘 작동하지만 IE와 crome에서는 그렇지 않습니다. 귀하의 경우 list에 몇 가지 대안jquery로 정렬하기

$("#points").click(function() {   
     var list = $('#categoryDetailedView>li'); 
     var sortcol = ".points"; 
     if ($(this).attr("data-sort") == "desc") { 
      list.sort(function (a, b) { 
       alert($(sortcol, a).html()); 
       return parseInt($(sortcol, a).html(), 10) > parseInt($('.points', b).html(), 10); 
      }); 
      $(this).attr("data-sort", "asc"); 
     } else { 
      list.sort(function (a, b) { 
       return parseInt($(sortcol, a).html(), 10) < parseInt($('.points', b).html(), 10); 
      }); 
      $(this).attr("data-sort", "desc"); 
     } 

     $('#categoryDetailedView').html(list); 
    }); 

답변

2

는 jQuery를 객체가 아닌 배열 제안, 그래서 정렬 방법은 없을 것 바랍니다. 시도

$("#points").click(function() { 
    var list = $('#categoryDetailedView>li').get(); 
    var sortcol = ".points"; 
    if ($(this).attr("data-sort") == "desc") { 
     list.sort(function (a, b) { 
      return parseInt($(sortcol, a).html(), 10) > parseInt($('.points', b).html(), 10) ? 1 : -1; 
     }); 
     $(this).attr("data-sort", "asc"); 
    } else { 
     list.sort(function (a, b) { 
      return parseInt($(sortcol, a).html(), 10) < parseInt($('.points', b).html(), 10) ? 1 : -1; 
     }); 
     $(this).attr("data-sort", "desc"); 
    } 

    $('#categoryDetailedView').append(list); 
}); 

데모 : Fiddle