2011-12-15 90 views
-1

我遇到了一個簡單的問題,並在Firefox錯誤控制檯中得到「a爲空」的例子。沒有警報顯示。如果我從DOM元素更改爲對象,它的工作原理是什麼?Jquery .data()問題存儲和檢索數據

<!DOCTYPE html> 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script> 
    $(document).ready(function() { 

    $('<div>').attr('id', 'test').appendTo('#ii').text('dfdfd'); 
    $('#test').data({'rr': 'data', 'tt': 55}); 
    alert($.param($('#test').data())); 
    }); 
</script> 
    <html> 
    <head> 

    </head> 
    <body> 
     <div id="ii"></div> 

    </body> 
</html> 
+0

爲什麼當你明確地將它附加到`#test`時,你希望數據被附加到`#ii`上? – Blazemonger 2011-12-15 14:33:52

+0

輸入示例,在上面修正。 – user1009749 2011-12-15 14:35:58

回答

0

它看起來像你的意思是從#test而不是#ii獲取數據:

$('<div>').attr('id', 'test').appendTo('#ii').text('some text'); 
$('#test').data({'ob1': 'some', 'ob2': 55}); 
alert($.param($('#test').data())); 

Example

如果不工作,很可能與ID #ii元素呢不存在。