2010-12-05 5 views
1

Comment puis-je analyser les attributs data- * avec JQuery? Est-ce que quelque chose comme $('a').attr('data-*').each(function(){...}); est possible? Y a-t-il une méthode simple?parse data- * with jquery

Répondre

0

Parlez-vous des attributs de données,

ce plugin metata jquery est vraiment bon

http://plugins.jquery.com/project/metadata 
3

Je pense que vous essayez de faire quelque chose comme ceci:

$('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... 
    }); 
}); 
+0

Il existe une différence entre [HTML5 'data- *' attributes] (http://ejohn.org/blog/html-5-data-attributes/) et jQuery ['data()'] (http://api.jquery.com/data/) stockage; Je pense que le PO pose des questions sur le premier et vous répondez pour le dernier. – Phrogz

+0

@Phrogz, http://cl.ly/313n2V430t19182M3Y1l –

+1

Je suis corrigé! Merci pour la clarification. – Phrogz

0

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: Notez qu'il n'est pris en charge que depuis jQuery 1.4.3.