如何通过 $.param
将 HTML 属性值用作 JQuery 对象?
<div data-sd="{a:1,b:1,c:2}"></div>
当我尝试这个时,我得到了奇怪的结果。
<script type="text/javascript">
var v = $.param($('div').data('sd'));
</script>
最佳答案
用双引号将对象的属性名称括起来,以便格式良好 JSON :
<div data-sd='{"a":1,"b":1,"c":2}'></div>
然后,$.param
将按预期返回 a=1&b=1&c=2
。 <强> DEMO 。
When the data attribute is an object (starts with '{') or array (starts with '[') then jQuery.parseJSON is used to parse the string; it must follow valid JSON syntax including quoted property names.
关于jquery - HTML 数据属性值 jQuery 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12437928/