Perdonami per non essere più specifico su questo. Ho un bug così strano. Dopo il caricamento del documento, eseguo il ciclo di alcuni elementi che avevano originariamente data-itemname=""e imposto quei valori usando .attr("data-itemname", "someValue").
Problema: quando più tardi faccio il loop di quegli elementi, se lo faccio elem.data().itemname, ottengo "", ma se lo faccio elem.attr("data-itemname"), ottengo "someValue". È come se il .data()getter di jQuery ottenga solo elementi impostati inizialmente per contenere un valore, ma se sono originariamente vuoti e successivamente impostati, .data()non ottiene il valore in seguito.
Ho provato a ricreare questo bug ma non sono stato in grado di farlo.
modificare
Ho ricreato il bug! http://jsbin.com/ihuhep/edit#javascript,html,live
Inoltre, frammenti dal link sopra ...
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")vero elem.data().itemname?
elem.data().itemname = somevalue;non cambia i dati sottostanti.)