【发布时间】:2022-01-07 05:05:03
【问题描述】:
感觉这应该很简单
我有一些这样的 html(我无法控制 html)
<div class="something" data-spec="['thing','another-thing','one-more-thing']">Stuff</div>
我正在尝试将数据规范作为数组读取,而不必将其分解为字符串,将其拆分然后将值推送到数组中。
如果我这样做:
let elem = $('.something')
let attr = elem[0].attributes['data-spec']
console.log(attr)
返回:
data-spec="['thing','another-thing','one-more-thing']"
有没有办法将 data-spec 读取为数组对象?
谢谢。
【问题讨论】:
-
更改您的输出以使其成为有效的 JSON? (将
'更改为")然后 JSON.parse。从源头修复它,而不是在运行时破解它。
标签: javascript jquery arrays object