इस पर अधिक विशिष्ट न होने के लिए मुझे क्षमा करें। मेरे पास ऐसा एक अजीब बग है। डॉक्स लोड होने के बाद, मैं कुछ तत्वों को देखता हूं जो मूल रूप से हैं data-itemname="", और मैंने उन मूल्यों का उपयोग करके सेट किया है .attr("data-itemname", "someValue")।
समस्या: जब मैं बाद में उन तत्वों के माध्यम से लूप करता हूं, अगर मैं करता हूं, तो elem.data().itemnameमुझे मिलता है "", लेकिन अगर मैं करता elem.attr("data-itemname")हूं, तो मुझे मिलता है "someValue"। यह ऐसा है जैसे कि jQuery का .data()गेटटर केवल उन तत्वों को प्राप्त करता है जो शुरू में कुछ मान सम्मिलित करने के लिए सेट किए गए हैं, लेकिन यदि वे मूल रूप से खाली हैं, और बाद में सेट हैं, तो .data()बाद में मान नहीं मिलता है।
मैं इस बग को फिर से बनाने की कोशिश कर रहा हूं, लेकिन नहीं कर पाया हूं।
संपादित करें
मैंने बग को फिर से बनाया है! http://jsbin.com/ihuhep/edit#javascript,html,live
इसके अलावा, ऊपर लिंक से स्निपेट्स ...
जे एस:
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")ना elem.data().itemname?
elem.data().itemname = somevalue;अंतर्निहित डेटा में बदलाव नहीं होता है।)