开发者

jQuery doesn't return data

开发者 https://www.devze.com 2023-02-11 19:24 出处:网络
I set data with jQuery.data() but I couldn\'t get it later. Code: jQuery(\'#test\').live(\'click\', function() {

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号