Wybaczcie, że nie jestem bardziej szczegółowy w tej sprawie. Mam taki dziwny błąd. Po załadowaniu dokumentu zapętlam niektóre elementy, które pierwotnie miały data-itemname="", i ustawiam te wartości za pomocą .attr("data-itemname", "someValue").
Problem: Kiedy później zapętlę te elementy, jeśli tak elem.data().itemname, otrzymam "", ale jeśli tak elem.attr("data-itemname"), otrzymam "someValue". To tak, jakby .data()getter jQuery pobierał tylko te elementy, które są początkowo ustawione tak, aby zawierały jakąś wartość, ale jeśli są pierwotnie puste, a później ustawione, później .data()nie pobiera wartości.
Próbowałem odtworzyć ten błąd, ale nie udało mi się.
Edytować
Odtworzyłem błąd! http://jsbin.com/ihuhep/edit#javascript,html,live
Ponadto fragmenty z powyższego linku ...
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")nie elem.data().itemname?
elem.data().itemname = somevalue;sposób nie zmienia podstawowych danych.)