using thymeleaf之七设置属性的值(th:attr/th:value/th:alt-title/th:attrappend/th:attrprepend/th:checked)

本文详细介绍了Thymeleaf模板引擎中属性表达式的使用方法,包括如何设置属性值、同时设置多个属性值、设置相同alt和title属性值等技巧,并提供了具体的代码示例。

原文来自:https://blog.youkuaiyun.com/sun_jy2011/article/details/40215423

7.1 th:attr

用于设置其他属性的值,但不是所有属性的值都能设置,如text。

[html]  view plain  copy
  1. <form action="subscribe.html" th:attr="action=@{/subscribe}">  
  2.   <fieldset>  
  3.     <input type="text" name="email" />  
  4.     <input type="submit" value="Subscribe me!" th:attr="value=#{subscribe.submit}"/>  
  5.   </fieldset>  
  6. </form>  

th:attr还可以同时设置多个属性的值,以逗号隔开

[html]  view plain  copy
  1. <img src="../../images/gtvglogo.png"   
  2.      th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />  

7.2 th:alt-title

用于设置 alt 和title属性的值相同的两个属性。

[html]  view plain  copy
  1. <img src="../../images/gtvglogo.png"   
  2.      th:src="@{/images/gtvglogo.png}" th:title="#{logo}" th:alt="#{logo}" />  

用th:alt-title后:

[html]  view plain  copy
  1. <img src="../../images/gtvglogo.png"   
  2.      th:src="@{/images/gtvglogo.png}" th:alt-title="#{logo}" />  

7.3 th:attrappend和th:attrprepend

th:attrappend属性值前缀,例如一个标签的类名为a,想要变为“a b”,即增加一个类样式,可以使用此属性.

[html]  view plain  copy
  1. <input type="button" value="Do it!" class="btn" th:attrappend="class=${' ' + cssStyle}" />  

th:attrprepend的用法则相反,
[html]  view plain  copy
  1. th:attrappend="class=${cssStyle+' '}"  

7.4 th:checked设置复选框的值

此为布尔值属性之一,以后会详细介绍其他属性,在此先介绍th:checked。

(1)选中为:

[html]  view plain  copy
  1. <input type="checkbox" name="active" th:checked="true" />  
(2)未选中为:

[html]  view plain  copy
  1. <input type="checkbox" name="noactive" th:checked="false" />  
thymeleaf解析时不会设置th:checked属性,即解析后为

[html]  view plain  copy
  1. <input type="checkbox" name="noactive" />  

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值