2011-10-19 15 views
4

配列を作成し、div要素に割り当て、値をpush()して後で再度取得しようとしています。jquery - dataを使用して格納された配列から値を取得および取得する方法

// set Array 
// $(this) is the HTML-element which should get array assigned 
var stack = [], 
    stack.push('#'+$(this).find(':jqmData(role="page")').attr('id')), 

$(this).data(stack); 

console.log($(this).data(stack)); 

を使用して

console.log($(this).data(stack[0] OR stack.length)); 
ながら、私は、全体のHTML要素を取り戻すよ:

誰かが私が...欠けているものを、教えてもらえます

は動作しません。

は、どのように私は(おそらくが...)、配列の中に押し込まの要素にアクセスすることができますか?

答えて

3

。たとえば、次のようにも

$("div").data("stack", []); //This sets the data key "stack" to an empty array 
$("div").data("stack").push(123); //This retrieves the array and pushes a number to the array 
console.log($("div").data("stack")); //This prints the array 
+0

興味深いアプローチを使用しています、1 –

+0

はどうもありがとうございました。完璧に動作します! – frequent

1

要素にデータを割り当てるには、インデックスを付けるキーを付ける必要があります。

PUT

$(this).data('stack', stack); 

は、データは、キー、値のペアを取る

var stack = $(this).data('stack'); 
+0

おかげで、私はXeon06の提案 – frequent

0

をGET。あなたは鍵を渡していません。

$(this).data('stack', stack); 

そして、それを取得する:あなたは、あなたのデータに名前を付ける必要がある

$(this).data('stack'); 
関連する問題