使用jQuery获取嵌套数据属性

时间:2013-04-30 21:36:43

标签: jquery html5

http://api.jquery.com/data/

我正在尝试使用数据方法做这样的事情:

$("body").data("bar", { myType: "test", count: 40 });

但是,我似乎无法像这样设置“计数”:

$("body").data(bar["count"], 41); 

我的语法错了吗?

2 个答案:

答案 0 :(得分:5)

语法为.data( key, value )data(bar["count"], 41);等于data(40, 41);

如果您想更改对象的值,可以执行$("body").data("bar").count = 41

答案 1 :(得分:0)

看起来你需要像这样使用它:

$("body").data({ baz: [ 1, 2, 3 ] });

// then

var data = $("body").data();
console.log(data.baz);  // outputs the array [ 1, 2, 3 ]
相关问题