我有一个 jsp 文件,像这样:
<html>
<head>
<script type="text/javascript">
var type=<bean:write name="class" property="type" />
</script>
<style type="text/css">
.td-type1
{
width: 10mm;
}
.td-type2
{
width: 20mm;
}
</style>
</head>
<body>
<table>
<tr>
<td class="td-type1">
</td>
</tr>
</table>
</body>
</html>
我的问题是:如何根据类型值动态更改 css? 例如,如果 type 等于 2,则 td 标签应使用 td-type2 的 css 类。 我应该使用 .properties 文件来保存所有配置或多 css 文件还是...?
最佳答案
您可以将请求属性的值附加到 JSP 中的 class
属性:
<td class="td-type<%=type%>">
作为旁注,强烈建议不要使用 scriptlet(JSP 中的 java 代码)。请改用 JSTL 和 EL。在这个问题中你会发现 Why and how to avoid Java Code in JSP files .
<td class="td-type${type}">
或者,如果你想实现一个类似 if-else 的结构,例如:
<c:choose>
<c:when test="${type eq "2"}">
<c:set var="varclass" value="td-type2"/>
</c:when>
<c:otherwise>
<c:set var="varclass" value="td-type1"/>
</c:otherwise>
</c:choose>
<td class="${varClass}">
关于java - 如何根据参数值在jsp文件中使用动态CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14775958/