存储和检索JavaScript数组和从HTML5数据属性 [英] store and retrieve javascript arrays into and from HTML5 data attributes

查看:267
本文介绍了存储和检索JavaScript数组和从HTML5数据属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪有一个JavaScript 阵列被存储在HTML5 数据属性?

How can a javascript Array be stored in an HTML5 data attribute?

我试过的每一个变化 JSON.stringify 阳离子和转义字符。

I've tried every variation of JSON.stringifycation 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='[&#34;some&#34;, &#34;string&#34;, &#34;here&#34;]'></div>

然后在JavaScript得到它无需任何额外的魔法:

And then in javascript get it without any additional magic:

var ar = $('#demo').data('stuff');

勾选此捣鼓出

这篇关于存储和检索JavaScript数组和从HTML5数据属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆