英文:
What's the difference between data-th-text and th:text in Spring thymeleaf
问题
我是春季模板引擎 Thymeleaf 的新手,我无法理解这两者 data-th-text
和 th:text
之间的区别。
有人能够通过示例解释一下这两者的不同吗?以及在什么情况下我应该使用 data-th-text
和 th: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:
<p th:text="#{home.welcome}">Welcome to our grocery store!</p>
This is HTML5 valid:
<p data-th-text="#{home.welcome}">Welcome to our grocery store!</p>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论