我需要在 html 页面中使用一些自定义 css,其中 css 样式是从数据库中检索的。当页面调用 Controller 时,会将 css 样式渲染为 html。我只想显示颜色代码而不是整个范围标签。
这是我需要的结果:
color: #159426;
这是我得到的意想不到的结果:
color: <span>#159426</span>;
这是我的目录结构。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title</title>
<meta content="initial-scale=1.0, width=device-width" name="viewport"/>
<style th:include="generic/templates/init :: init" th:with="params=${params}"/>
</head>
<body>
<div id="wrapper">
<a><h1>....111</h1></a>
<h1>....222</h1>
</div>
</body>
</html>
这是 init.html
<script th:inline="javascript" th:fragment="init">
h1, h2, h3, h4, h5, h6, .site-title {
font-family: 'Open Sans', sans-serif;
}
body.site {
border-top: 3px solid <span th:text="${params.templateColor}"/>;
background-color: <span th:text="${params.templateBackgroundColor}"/>;
}
a {
color: <span th:text="${params.templateColor}"/>;
}
</script>
这是我的结果
<!DOCTYPE html>
<html xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title</title>
<meta content="initial-scale=1.0, width=device-width" name="viewport" />
<style>
h1, h2, h3, h4, h5, h6, .site-title {
font-family: 'Open Sans', sans-serif;
}
body.site {
border-top: 3px solid <span>#159426</span>;
background-color: <span>#f4f6f7</span>;
}
a {
color: <span>#159426</span>;
}
</style>
</head>
<body>
<div id="wrapper">
<a><h1>....111</h1></a>
<h1>....222</h1>
</div>
</body>
</html>
最佳答案
尝试使用
th:remove="tag"
例如替代
a {
color: <span th:text="${params.templateColor}" />;
}
用这个
a {
color: <span th:text="${params.templateColor}" th:remove="tag"/>;
}
关于Thymeleaf 在没有span标签的html上打印实体属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46275498/