parse data-* with jquery
How can I parse data-* attri开发者_C百科butes with JQuery? Is something like $('a').attr('data-*').each(function(){...});
possible? Is there a simple method?
I think you're trying to do something like this:
$('a').filter(function() {
//if this function returns false, it will not be included in the set.
return $(this).data().length > 0;
}).each(function() {
//iterate over every matched DOM element
//and iterate over their data attribute:
$.each($(this).data(), function(key, value) {
//do something with key and value here...
});
});
Are you talking about data attributes ,
this jquery metata plugin is really good
http://plugins.jquery.com/project/metadata
Try this http://jsfiddle.net/6tv5y/1
<div id="mydiv" data-name="negative" data-from="stackoverflow"></div>
var mydata = $('#mydiv').data();
$.each(mydata ,function(i) {
alert(mydata[i]);
});
UPD: Note that it is supported only since jQuery 1.4.3.
精彩评论