th:utext和th:text
时间: 2023-08-27 10:54:34 浏览: 174
在 Thymeleaf 模板引擎中,`th:utext` 和 `th:text` 是用于处理文本内容的属性。
`th:utext` 用于将变量值作为 HTML 解析并渲染到页面上。它会自动对变量值中的 HTML 标签进行解析,而不是将其作为纯文本呈现。这在需要渲染富文本内容时非常有用。
示例:
```html
<p th:utext="${content}"></p>
```
假设 `content` 变量的值为 `<strong>Hello World!</strong>`,则使用 `th:utext` 属性后会将其解析为加粗的 "Hello World!"。
`th:text` 用于将变量值作为纯文本呈现到页面上,不会对其中的 HTML 标签进行解析。
示例:
```html
<p th:text="${content}"></p>
```
假设 `content` 变量的值为 `<strong>Hello World!</strong>`,则使用 `th:text` 属性后会将其作为普通文本显示,即显示 `<strong>Hello World!</strong>`。
相关问题
<h3 id="lyrics">歌词</h3> <hr style="border: none; height: 3px; width: 80%; background-color: black; margin: 0 10%;"/> <div class="mt-4 mb-3 content" id="md-content" style="text-align: center"> <textarea style="display: none" th:utext="${music.lyric}"></textarea> </div>如果是这样传入歌词呢?
如果你想通过一个 `<textarea>` 元素来传入歌词,并实现歌词滚动,可以将 HTML 模板中的歌词部分修改成以下代码:
```html
<h3 id="lyrics">歌词</h3>
<hr style="border: none; height: 3px; width: 80%; background-color: black; margin: 0 10%;"/>
<div class="mt-4 mb-3 content" id="md-content" style="text-align: center">
<div id="lyrics">
<!-- 使用模板语法获取歌词 -->
<p v-for="(line, index) in lyricLines" :key="index">{{ line }}</p>
</div>
</div>
<textarea id="lyric-textarea" style="display: none">{{ music.lyric }}</textarea>
```
在这个代码中,我们使用了一个 `<textarea>` 元素来存储歌词文本,然后通过 Vue.js 的模板语法 `v-for` 来遍历歌词文本中的每一行,并将它们分别显示在一个 `p` 元素中。这里需要在 Vue 实例中定义一个计算属性 `lyricLines`,它会将歌词文本分割成行数组,并返回该数组。由于 `<textarea>` 元素的内容会被转义,因此我们使用了 `th:utext` 来告诉 Thymeleaf 不要转义文本。
下面是 Vue 实例的代码:
```js
new Vue({
el: '#md-content',
data: {
music: {
// ...
},
lyricText: '',
},
computed: {
// 将歌词文本分割成行数组
lyricLines: function() {
return this.lyricText.split('\n');
},
},
mounted: function() {
// 获取歌词文本
this.lyricText = document.querySelector('#lyric-textarea').value;
// 监听播放器的 timeupdate 事件
var player = document.querySelector('#player');
var lyrics = document.querySelector('#lyrics');
var lyricLines = this.lyricLines;
player.addEventListener('timeupdate', function() {
// 计算当前播放时间对应的歌词行
var currentTime = this.currentTime;
for (var i = 0; i < lyricLines.length; i++) {
var match = lyricLines[i].match(/^\[(\d{2}):(\d{2})\.(\d{2})\](.*)$/);
if (match && match[1] && match[2] && match[3] && match[4]) {
var time = parseInt(match[1]) * 60 + parseInt(match[2]) + parseInt(match[3]) / 100;
if (currentTime >= time && (!lyricLines[i + 1] || currentTime < parseInt(lyricLines[i + 1].match(/^\[(\d{2}):(\d{2})\.(\d{2})\]/)[1]) * 60 + parseInt(lyricLines[i + 1].match(/^\[(\d{2}):(\d{2})\.(\d{2})\]/)[2]) + parseInt(lyricLines[i + 1].match(/^\[(\d{2}):(\d{2})\.(\d{2})\]/)[3]) / 100)) {
// 当前播放时间对应的歌词行
var currentLine = i;
break;
}
}
}
// 滚动到当前歌词行
var currentElement = lyrics.querySelectorAll('p')[currentLine];
if (currentElement) {
lyrics.scrollTop = currentElement.offsetTop - lyrics.offsetTop;
}
});
},
});
```
在这个代码中,我们使用了 Vue.js 的生命周期钩子函数 `mounted` 来在组件挂载后获取歌词文本,并监听播放器的 `timeupdate` 事件来动态更新歌词滚动。由于 `<textarea>` 元素的内容会被转义,我们需要在 `mounted` 钩子函数中通过 `document.querySelector('#lyric-textarea').value` 来获取原始的歌词文本。
th:text 转义html标签
在 Thymeleaf 中,使用 th:text 属性设置文本时,会自动对文本进行 HTML 转义,以避免 XSS 攻击。但有时候我们需要在页面上显示 HTML 标签,而不是将其解释为文本。这时可以使用 th:utext 属性,它会将文本作为 HTML 解释并显示在页面上。
例如,如果我们有一个字符串变量 content,其中包含 HTML 标签,我们可以这样显示它:
```
<div th:utext="${content}"></div>
```
这样会将 content 中的 HTML 标签解释并显示在页面上。但要注意,使用 th:utext 会增加页面的风险,因为它可以使页面容易受到 XSS 攻击。因此,必须确保在显示用户输入内容时进行适当的过滤和转义,以保护页面的安全。
阅读全文
相关推荐















