简体   繁体   中英

Fiddle doesn't work Javascript

http://jsfiddle.net/9zvt6/

var SList = $('.table>div.conChip').sort(function(a,b){
   return a.dataset.sid > b.dataset.sid
});

var i = 0;
while(SList.length>i)
{
  alert(SList[i].dataset.sid);
  i++;
}

If I remove the inner divs the styles and classes everything seems to work, otherwise it doesn't, what am I missing?

Change your sort, sort need to return negative, positive, zero to sort the reposition the element.

from

var SList = $('.table>div.conChip').sort(function(a,b){
   return a.dataset.sid > b.dataset.sid
});

to

var SList = $('.table>div.conChip').sort(function (a, b) {
    return a.dataset.sid - b.dataset.sid; 
    //for support in older browsers use jquery data api or getAttribute
    //return $(a).data("sid") - $(b).data("sid");
    //return a.getAttribute("data-sid") - b.getAttribute("data-sid");
});

Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM