[英]How to change the class of an element depending on the value on the JSON
我在后端有一个来自 python 的 JSON 文件,它的结构非常基本,例如:
{"thing": "ok",
"anotherthing": "fault"}
我正在使用 bootstrap span 类徽章。 现在,如果 JSON 中的值是“ok”,那么我想显示一个绿色徽章,如果它的“错误”,那么我想显示一个红色徽章。
<span>Thing</span>
<span class="badge badge-pill badge-success">Operational</span>
<span>AnotherThing</span>
<span class="badge badge-pill badge-danger">Major Fault</span>
现在的问题是,如果在后端“故障”更改为“正常”,我该如何让它显示成功徽章? 所以基本上它动态渲染元素。 我可以通过执行<span>{{message}}</span>
在 HTML 上显示我的整个 JSON。 这将显示 JSON 文件。 我可能需要在这里使用 Javascript,但我不知道如何继续。 此外,执行 ```{{message[0]}}" 只显示 "{",它是 JSON 字符串的开头,而不是应该是 "thing" 的第一个元素。
<!doctype html>
<html lang="en">
<head>
<title>{{ title }} - Status Checker</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles/styles.css') }}">
<script>
const message = JSON.parse(message)
const TestDrive = document.getElementById('TestDrive')
if (message[0].TestDrive === 'OK') {
TestDrive.classList.add('badge-success')
}
</script>
</head>
<body>
<ul>
<li>
<span>TestDrive</span>
<span id="TestDrive" class="badge badge-pill">Operational</span>
</li>
</ul>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
您需要解析您从 javascript 中的 python 文件获取的 JSON 数据。
const message = JSON.parse({{your_data_from_python_file}})
在这里,我假设数据将是对象数组。 message[0] --> 它会返回你的第一个元素,它是一个对象。 使用点表示法从中提取值。
const message = [{ "thing": "ok", "anotherthing": "fault" }] // get the thing's span using Id first const first = document.getElementById('first') if (message[0].thing === 'ok') { // now add class in thing's span first.classList.add('badge-success') } /*get the Anotherthing's span using Id second same is present in html file*/ const second = document.getElementById('second') if (message[0].anotherthing === 'fault') { second.classList.add('badge-danger') }
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" /> <span>Thing</span> <span id="first" class="badge badge-pill">Operational</span> <span>AnotherThing</span> <span id="second" class="badge badge-pill">Major Fault</span>
<script>
const message= {"thing": "ok",
"anotherthing": "fault"};
const classList = message.thing==="ok" && "success" || "fault"
document.getElementById("TestDrive").classList.add(`badge-${classList}`);
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.