gpt4 book ai didi

jquery - HTML 数据属性值 jQuery 对象

转载 作者:行者123 更新时间:2023-12-01 07:22:22 25 4
gpt4 key购买 nike

如何通过 $.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

来自HTML5 data-* Attributes :

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com