html - thymeleaf 选择背景颜色
问题描述
我有以下 HTML:
<select
th:field="*{status.health}"
th:value="${status.health}"
th:class="health_color"
th:name="status_health">
<option value="-1">Select Health</option>
<option th:class="health_color" th:value="green" th:text="green"></option>
<option th:class="health_color" th:value="yellow" th:text="yellow"></option>
<option th:class="health_color" th:value="red" th:text="red"></option>
</select>
以及下面的 JavaScript
$('.health_color').each(function() {
if ($(this).text() == 'red') {
$(this).css('background-color', '#ff5050');
} else if ($(this).text() == 'yellow') {
$(this).css('background-color', '#ffd24d')
} else if ($(this).text() == 'green') {
$(this).css('background-color', '#80ffaa')
}
});
上面的样式显示在页面的所有其他元素中,包括选择选项,但不显示选择框本身。我究竟做错了什么?
谢谢。
解决方案
我相信该函数期望从该select
字段没有的文本属性中读取颜色。
我最好的猜测是,当$(this).text()
为 运行时select
,它返回里面所有文本属性的组合属性。
下面是测试我的理论的代码片段。
请记住,您始终可以使用 chrome 开发人员工具(例如)调试您的代码,它可以让您更好地了解它发生了什么。
$('.health_color').each(function() {
console.log($(this).text());
if ($(this).text() == 'red') {
$(this).css('background-color', '#ff5050');
} else if ($(this).text() == 'yellow') {
$(this).css('background-color', '#ffd24d')
} else if ($(this).text() == 'green') {
$(this).css('background-color', '#80ffaa')
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="health_color"
name="status_health">
<option value="-1">Select Health</option>
<option class="health_color" value="green" text="green">green</option>
<option class="health_color" value="yellow" text="yellow">yellow</option>
<option class="health_color" value="red" text="red">red</option>
</select>
推荐阅读
- excel - 基于值长度的格式条件
- python - 如何获取 json 文件中的一些数据?
- python - 在嵌套循环中返回 2 步
- c++ - 在一个范围内查找彼此之间具有不同最小距离的随机数
- php - 循环遍历数组
- angular - 如何将变量传递给代理服务器 url?
- flutter - 即使图像应该在限制范围内,也会从缓存中删除图像
- c++ - Visual Studio 代码#include 停止工作
- android - 如何使用带有 Kotlin 的 Room 和 moshi 持久化带有 JSON 数组的 JSON 对象
- android - 是否可以为我的测试返回 http 响应代码 = 200?