简体   繁体   中英

Alternative to dataset for IE8 (sorting elements by data attribute)

Can anyone help me get this script working for IE8? It works fine in Mozilla and WebKit browsers, but throws an error in IE8: 'dataset.numValue' is null or not an object.

var $wrapper = $('#containerInner'),
  $sorted = $wrapper.find('.sortMe').get().sort(function (a, b) {
    return +a.dataset.numValue - +b.dataset.numValue;
  });

I know IE8 supports data attributes... but can't figure out or find on Google an answer to this!

You should try using data() of jQuery instead of dataset.

var $wrapper = $('#containerInner'),
  $sorted = $wrapper.find('.sortMe').get().sort(function (a, b) {
    return +$(a).data("numValue") - + $(b).data("numValue");
});

You should use: $(el).data('key') . To store data on an element using markup simply use the data-key attributes.

在jQuery中,您可以使用$ .data方法来获取此值。

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