admin管理员组文章数量:1353255
I was wondering if it is possible to store multiple key/values inside a data-attr
like:
<a href="#" data-social="network:facebook; socialId:123456789" id="facebook">Facebook</a>
and then when using them in my script just pick at them like $('#facebook').data('social.socialId');
or something like that instead of having to break each of them up into their own attribute?
I was wondering if it is possible to store multiple key/values inside a data-attr
like:
<a href="#" data-social="network:facebook; socialId:123456789" id="facebook">Facebook</a>
and then when using them in my script just pick at them like $('#facebook').data('social.socialId');
or something like that instead of having to break each of them up into their own attribute?
4 Answers
Reset to default 13You don't need to restrict it either to only one data-*
attribute. You can always use as many as you need.
<a href="#" data-social-network="facebook" data-social-id="123456789" id="facebook">Facebook</a>
var socialId = $('#facebook').data('social-id');
But, if you still insist. It is possible to store a JSON string and parse it.
<a href='#' data-social='{ "network": "facebook", "socialId": 123456789 }' id='facebook'>Facebook</a>
var socialId = $('#facebook').data('social').socialId;
The JSON Object is automatically parsed by jQuery.
you could store data in JSON format in the attribute like
<div id="myElement" data-json='["value1", "value2", "value3"]'></div>
and then retrieve it like
var data = JSON.parse($('#myElement').attr('data-json'));
Store them as an object.
$('#facebook').data('social', { network: 'facebook', socialId: 12345679 });
You can use JSON and that's automatically parsed, I believe
<a href="#" data-social="{network:facebook; socialId:123456789}" id="facebook">Facebook</a>
Would make possibilities for
$('#facebook').data('social').socialId;
本文标签: javascriptis it possible to store multiple keyvalues inside 1 data attributeStack Overflow
版权声明:本文标题:javascript - is it possible to store multiple keyvalues inside 1 data attribute? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743926815a2563112.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论