Perdóname por no ser más específico en esto. Tengo un error tan extraño. Después de que se carga el documento, hago un bucle con algunos elementos que originalmente tenían data-itemname=""y configuro esos valores usando .attr("data-itemname", "someValue").
Problema: cuando luego recorro esos elementos, si lo hago elem.data().itemname, obtengo "", pero si lo hago elem.attr("data-itemname"), obtengo "someValue". Es como si el .data()captador de jQuery solo obtiene elementos que están configurados inicialmente para contener algún valor, pero si originalmente están vacíos y luego configurados, entonces .data()no obtiene el valor más adelante.
He intentado recrear este error, pero no he podido.
Editar
¡He recreado el error! http://jsbin.com/ihuhep/edit#javascript,html,live
Además, fragmentos del enlace anterior ...
JS:
var theaters = [
{ name: "theater A", theaterId: 5 },
{ name: "theater B", theaterId: 17 }
];
$("#theaters").html(
$("#theaterTmpl").render(theaters)
);
// DOES NOT WORK - .data("name", "val") does NOT set the val
var theaterA = $("[data-theaterid='5']");
theaterA.find(".someLink").data("tofilllater", "theater5link"); // this does NOT set data-tofilllater
$(".someLink[data-tofilllater='theater5link']").html("changed link text"); // never gets changed
// WORKS - .attr("data-name", "val") DOES set val
var theaterB = $("[data-theaterid='17']");
theaterB.find(".someLink").attr("data-tofilllater", "theater17link"); // this does set data-tofilllater
$(".someLink[data-tofilllater='theater17link']").html("changed link text");
HTML:
<body>
<div id="theaters"></div>
</body>
<script id="theaterTmpl" type="text/x-jquery-tmpl">
<div class="theater" data-theaterid="{{=theaterId}}">
<h2>{{=name}}</h2>
<a href="#" class="someLink" data-tofilllater="">need to change this text</a>
</div>
</script>
elem.data("itemname")así elem.data().itemname?
elem.data().itemname = somevalue;lo tanto , no cambia los datos subyacentes)