简体   繁体   中英

jQuery doesn't return data

I set data with jQuery.data() but I couldn't get it later. Code:

jQuery('#test').live('click', function() {
    alert(jQuery.data($(this), 'key'));
});
var element = jQuery('<div id="test">Test</div>');
jQuery.data(element, 'key', { test: "String" });
jQuery('body').append(element);

I'm using jQuery 1.5. Is this a bug (in jQuery .live() ) or am I doing something wrong?

Try this instead:

$('#test').live('click', function() {
    alert($(this).data('key'));
});
var element = $('<div id="test">Test</div>');
$(element).data('key', { test: "String" });
$('body').append(element);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM