Created
September 17, 2013 18:37
-
-
Save mach3/6598728 to your computer and use it in GitHub Desktop.
html要素に設定した属性を全て取得するやつ
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
(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