Last active
December 17, 2015 02:19
-
-
Save htsign/5535316 to your computer and use it in GitHub Desktop.
HTML5の独自データ属性について、Element.dataset.***でアクセスできて欲しいのにIEではできないので、それをできるようにしようと思って書いた。 ECMAScript5th準拠なのでIE9以降でないと動作しません。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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