2 回答

TA贡献1775条经验 获得超8个赞
您可以使用以下功能来完成您的任务:
element.getAttribute
获取属性内的值element.setAttribute
设置属性内的值JSON.stringify
并JSON.parse
使用 JSON
检查下面的代码。
var meter1 = document.getElementById("meter1")
var dataSettings = JSON.parse(meter1.getAttribute("data-settings"))
dataSettings.value = 8
dataSettings.min = 5
meter1.setAttribute("data-settings", JSON.stringify(dataSettings))
console.log(meter1.getAttribute("data-settings"))
<div class="gauge" id="meter1" data-settings='
{"value": 7,
"min": 0,
"max": 50,
"threshold": [
{"from": 25, "to": 50, "color": "blue", "label": "Warning"},
{"from": 0, "to": 25, "color": "orange", "label": "Critical"}
]
}'></div>

TA贡献1811条经验 获得超6个赞
您可以document.querySelector使用JSON.parse
const gauge = document.querySelector('#meter1');
console.log(JSON.parse(gauge.dataset.settings).threshold)
<div class="gauge" id="meter1" data-settings='
{"value": 7,
"min": 0,
"max": 50,
"threshold": [
{"from": 25, "to": 50, "color": "blue", "label": "Warning"},
{"from": 0, "to": 25, "color": "orange", "label": "Critical"}
]
}'></div>
添加回答
举报