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

7.1 th:attr

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

<form action="subscribe.html" th:attr="action=@{/subscribe}">
  <fieldset>
    <input type="text" name="email" />
    <input type="submit" value="Subscribe me!" th:attr="value=#{subscribe.submit}"/>
  </fieldset>
</form>

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

<img src="../../images/gtvglogo.png" 
     th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />

7.2 th:alt-title

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

<img src="../../images/gtvglogo.png" 
     th:src="@{/images/gtvglogo.png}" th:title="#{logo}" th:alt="#{logo}" />

用th:alt-title后:

<img src="../../images/gtvglogo.png" 
     th:src="@{/images/gtvglogo.png}" th:alt-title="#{logo}" />

7.3 th:attrappend和th:attrprepend

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

<input type="button" value="Do it!" class="btn" th:attrappend="class=${' ' + cssStyle}" />

th:attrprepend的用法则相反,
th:attrappend="class=${cssStyle+' '}"

7.4 th:checked设置复选框的值

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

(1)选中为:

<input type="checkbox" name="active" th:checked="true" />
(2)未选中为:

<input type="checkbox" name="noactive" th:checked="false" />
thymeleaf解析时不会设置th:checked属性,即解析后为

<input type="checkbox" name="noactive" />



  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值