http://jsfiddle.net/673h38g9/17/
我正在尝试通过Jquery mobile获取数据属性数组,但它表示未定义。
<div data-stuff="['a','b','c']"></div>
var vendoritems = $('div').data('stuff');
alert(vendoritems);
var myid = 'd';
if (vendoritems.indexOf(myid) > -1) {
$('div').data('stuff', vendoritems.push(myid))
var vendoritemsss = $('div').data('stuff');
}
JQuery Mobile是否不支持数据attr数组?
答案 0 :(得分:1)
你必须
onDomReady
工作小提琴:http://jsfiddle.net/673h38g9/18/
var vendoritems = $('#data').data('stuff');
alert(vendoritems);
var myid = 'd';
if (vendoritems.indexOf(myid) < 0) {
vendoritems.push(myid);
$('#data').data('stuff', vendoritems);
var vendoritems2 = $('#data').data('stuff');
alert(vendoritems2);
}
HTML:
<div id="data" data-stuff='["a","b","c"]'></div>