存储和检索JavaScript数组和从HTML5数据属性 [英] store and retrieve javascript arrays into and from HTML5 data attributes
问题描述
哪有一个JavaScript 阵列
被存储在HTML5 数据
属性?
How can a javascript Array
be stored in an HTML5 data
attribute?
我试过的每一个变化 JSON.stringify
阳离子和转义字符。
I've tried every variation of JSON.stringify
cation and escaping characters.
什么是存储阵列并再次检索它的precise方法?
What is the precise method to store the array and retrieve it again?
注意
我建立与数组[$(#firstSelectedElement)。VAL(),$(#secondSelectedElement)。VAL()]
。我找回 ID =storageElement数据storeIt =东西
与 $(#storageElement)。数据(storeit')
。
我似乎从来没有检索数据作为一个真正的阵列
,只有一个阵列
字符。
I can never seem to retrieve the data as a true Array
, only an Array
of characters.
推荐答案
原来,你可以使用HTML元素中的转义字符数据
属性有json-像阵列(EN codeD有引号):
It turned out that you could use the html escaped characters in the element data
attribute to have json-like array (encoded are quotes):
<div id="demo" data-stuff='["some", "string", "here"]'></div>
然后在JavaScript得到它无需任何额外的魔法:
And then in javascript get it without any additional magic:
var ar = $('#demo').data('stuff');
勾选此捣鼓出
这篇关于存储和检索JavaScript数组和从HTML5数据属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!