Created
November 16, 2010 10:07
-
-
Save shawnchin/701652 to your computer and use it in GitHub Desktop.
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
// return data-* attributes of a node as a dict | |
function getDataAttributes(node) { | |
var vstr = $().jquery.split("."); | |
var v0 = parseInt(vstr[0]) || 0, | |
v1 = parseInt(vstr[1]) || 0, | |
v2 = parseInt(vstr[2]) || 0; | |
// jQuery >= 1.4.4 has built in op to do this. | |
if ( v0 > 1 || (v0 == 1 && v1 > 4) || (v0 == 1 && v1 == 4 && v2 >= 4)) { | |
return node.data(); | |
} | |
// for jQuery < 1.4.4, work it out ourselves, | |
var d = {}, re_dataAttr = /^data\-(.+)$/; | |
$.each(node.get(0).attributes, function(index, attr) { | |
if (re_dataAttr.test(attr.nodeName)) { | |
var key = attr.nodeName.match(re_dataAttr)[1]; | |
d[key] = attr.nodeValue; | |
} | |
}); | |
// merge in data added/modified via $.fn.data (>1.4 only) | |
if (v0 == 1 && v1 == 4)) { $.extend(d, node.data()); } | |
return d; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ZhangYiJiang: I attempted to make it version independent as it's part of a plugin that I plan to use in different projects (one of which were developed against 1.4.2 and not tested with 1.4.4). However, I do see your point.
In retrospect, it does seem rather pointless to include both approaches when one would work for both pre/post 1.4.4 versions.
Many thanks.