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<开发者_JAVA技巧;/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);
精彩评论