thymeleaf 添加 html 转义字符

问题描述 投票:0回答:1

我正在尝试向 plausibles api 发送一个事件来跟踪收入。问题是百里香逃避了价格。这会导致api无法获取正确的信息。

问题来自[[${result.price}]] 结果是:“1234” 只需输入 1234 作为 amount: 1234 即可将结果正确存储在 api 中。

调用窗口函数的代码

    <div class="product-card" th:each="result : ${searchResults}">
        <div class="product-image">
            
            <a th:href="@{${result.url}}" target="_blank" rel="noopener noreferrer"><img
                    class="product-thumb plausible-event-name=product+click"
                    th:onclick="window.plausible('product click', {revenue: {currency: 'SEK', amount: [[${result.price}]] }})"
                th:src="${result.imageUrl}" onerror="this.src='Product-Image-Coming-Soon.jpg'" alt=""></a>

        </div>

返回结果

<img class="product-thumb plausible-event-name=product+click" onclick="window.plausible('product click', {revenue: {currency: 'SEK', amount: quot;1234quot; }})" src="https://cdn-1.xxx.com/UMPROhsfQv4SWki4SpOhY9O6hydA9CI09YTchmT2mbQ.jpeg" onerror="this.src='Product-Image-Coming-Soon.jpg'" alt="">
thymeleaf spring-thymeleaf plausible-analytics
1个回答
0
投票

在 javascript 内联中使用 thymeleaf 时

[[${var}]]
会转义字符。如果你不想在 javascript 中转义字符,你应该使用
[(${var})]

有关内联的更多信息,请参阅 Thymeleaf 文档 https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#inlined

© www.soinside.com 2019 - 2024. All rights reserved.