因为在IE7elem.properties中列出了所有可能的属性,而不仅仅是当前的属性,所以我们必须测试属性值。此插件适用于所有主要浏览器:(function($) {
$.fn.getAttributes = function () {
var elem = this,
attr = {};
if(elem && elem.length) $.each(elem.get(0).attributes, function(v,n) {
n = n.nodeName||n.name;
v = elem.attr(n); // relay on $.fn.attr, it makes some filtering and checks
if(v != undefined && v !== false) attr[n] = v })
return attr }})(jQuery);
用法:var attribs = $('#some_id').getAttributes();