jQuery: leggere una stringa JSON negli attributi di dati come oggetto

jQuery: leggere una stringa JSON negli attributi di dati come oggetto

jQuery è in grado di trasformare una stringa JSON contenuta in un attributo di dati in un oggetto.

Supponiamo di avere questo elemento HTML:


<div id="test" data-test='{"test": 1}'></div>

In jQuery avremo:


var $test = $( "#test" );
var test = $test.data( "test" );
console.log( test.test ); // 1

Torna su