Skip to content

Instantly share code, notes, and snippets.

@mach3
Created September 17, 2013 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mach3/6598728 to your computer and use it in GitHub Desktop.
Save mach3/6598728 to your computer and use it in GitHub Desktop.
html要素に設定した属性を全て取得するやつ
(function($, undefined){
/**
* Get all custom attributes
* @param HTMLElement el
* @return Object
*/
$.getAttributes = function(el){
var $el, data, add, i;
$el = $(el);
data = {};
add = function(attr){
if(! attr.nodeValue || $el.attr(attr.name) === undefined){ return; } // For IE7
data[attr.name] = attr.nodeValue;
};
for(i=0; i<el.attributes.length; i++){
add(el.attributes[i]);
}
return data;
};
/**
* jQuery interface
*/
$.fn.attrs = function(){
return $.getAttributes(this.get(0));
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment