1. 程式人生 > >thymeleaf之設定屬性的值

thymeleaf之設定屬性的值

7.1 th:attr

用於設定其他屬性的值,但不是所有屬性的值都能設定,如text。

 

 
  1. <form action="subscribe.html" th:attr="[email protected]{/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還可以同時設定多個屬性的值,以逗號隔開

 

 
  1. <img src="../../images/gtvglogo.png"

  2. th:attr="[email protected]{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />


7.2 th:alt-title

用於設定 alt 和title屬性的值相同的兩個屬性。

 

 
  1. <img src="../../images/gtvglogo.png"

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


用th:alt-title後:

 

 
  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”,即增加一個類樣式,可以使用此屬性.

 

<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" />

--------------------- 本文來自 sunjingyan 的CSDN 部落格 ,全文地址請點選:https://blog.csdn.net/sun_jy2011/article/details/40215423?utm_source=copy