“data-th-text”和”th:text”在Spring Thymeleaf中有什么区别?

huangapple go评论82阅读模式
英文:

What's the difference between data-th-text and th:text in Spring thymeleaf

问题

我是春季模板引擎 Thymeleaf 的新手,我无法理解这两者 data-th-textth:text 之间的区别。

有人能够通过示例解释一下这两者的不同吗?以及在什么情况下我应该使用 data-th-textth:text

英文:

I am new in spring thymeleaf and I am not able to get difference between these two data-th-text and th:text.

Can anyone explain the difference, with the help of examples, and when should I use data-th-text and th:text ?

答案1

得分: 2

两者都执行相同的操作,但根据Thymeleaf文档:

th:*表单中使用的非标准属性不符合HTML5规范。为了使您的模板符合HTML5规范,对属性名称使用data-前缀,对分隔符使用连字符(-),而不是分号(:)。

参考:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#using-texts

这不符合HTML5规范:

 <p th:text="#{home.welcome}">Welcome to our grocery store!</p>

这符合HTML5规范:

<p data-th-text="#{home.welcome}">Welcome to our grocery store!</p>
英文:

Both does the same thing but as per thymeleaf docs:

>Non-standard attributes we are using in the th:* form are not allowed by the HTML5 specification. To make your templates HTML5-valid use data- prefix for attribute names and hyphen (-) separators instead of semi-colons (:)

Ref: https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#using-texts

This is not HTML5 valid:

 &lt;p th:text=&quot;#{home.welcome}&quot;&gt;Welcome to our grocery store!&lt;/p&gt;

This is HTML5 valid:

&lt;p data-th-text=&quot;#{home.welcome}&quot;&gt;Welcome to our grocery store!&lt;/p&gt;

huangapple
  • 本文由 发表于 2020年5月30日 14:30:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/62098700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定