jquery-如何从使用data()存储的数组中设置和检索值
我试图创建一个数组,将其分配给div元素,推送()一个值,然后在稍后的阶段再次检索它 谁能告诉我,我错过了什么…:jquery-如何从使用data()存储的数组中设置和检索值,jquery,arrays,Jquery,Arrays,我试图创建一个数组,将其分配给div元素,推送()一个值,然后在稍后的阶段再次检索它 谁能告诉我,我错过了什么…: // 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 ); 使用 我将
// 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 );
使用
我将返回整个HTML元素,而
console.log( $(this).data(stack[0] OR stack.length) );
不要工作
如何访问我(大概是…)推送到数组中的元素?要将数据分配给元素,必须给它一个索引键 PUT
$(this).data('stack', stack);
var stack = $(this).data('stack');
获取
$(this).data('stack', stack);
var stack = $(this).data('stack');
数据采用密钥、值对。你不能把钥匙递进去
$(this).data('stack', stack );
然后检索它:
$(this).data('stack');
您需要命名您的数据。例如:
$("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
非常感谢你。很好用!