Skip to content

Instantly share code, notes, and snippets.

@htsign
Last active December 17, 2015 02:19
Show Gist options
  • Save htsign/5535316 to your computer and use it in GitHub Desktop.
Save htsign/5535316 to your computer and use it in GitHub Desktop.
HTML5の独自データ属性について、Element.dataset.***でアクセスできて欲しいのにIEではできないので、それをできるようにしようと思って書いた。 ECMAScript5th準拠なのでIE9以降でないと動作しません。
window.DOMStringMap || Object.defineProperty(Element.prototype, "dataset", {
enumerable: false,
configurable: false,
get: function(){
var dataset = {};
Array.prototype.slice.call(this.attributes)
.filter(function(val){
return val.name.indexOf("data-") === 0;
}).forEach(function(val){
var property = val.name.slice(5);
property = property.replace(/-(\w)/g, function($0){
return $0.charAt(1).toUpperCase();
});
dataset[property] = val.value;
});
return dataset;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment