【问题标题】:highlight text in html not working突出显示html中的文本不起作用
【发布时间】:2017-02-28 07:15:55
【问题描述】:

我想突出显示从服务器返回的文本,如下所示

This is my name <em>John</em>

我在服务器中调试,返回上面的字符串。 但是文本在浏览器中呈现时没有突出显示。

如下图所示

"This is my name <em>John</em>"

浏览器视图源码如下图

This is my name &lt;em&gt;John&lt;/em&gt;

我应该转义“”符号吗? . 我在这个应用程序中使用 spring mvc 和 thymeleaf 来生成 html

<div th:if="${not #lists.isEmpty(results)}">
                    <h3>Search Results</h3>
                    <table class="table table-striped">
                        <tr>
                            <th>Id</th>
                            <th>Title</th>

                        </tr>
                        <tr th:each="result : ${results}">
                            <td th:text="${result.id}">Id</td>
                            <td th:text="${result.title}">Title</td>

                        </tr>
                    </table>

                </div>

【问题讨论】:

  • 您应该更改 HTML 页面上的编码,或者您应该使用 Java 中的 StringEscapeUtils 来转义您的文本。
  • 这取决于您在浏览器中的显示方式。如果您使用的是 jquery,请尝试使用 $('id').html() 而不是 text()

标签: html spring-mvc highlight highlighting


【解决方案1】:

这就是解决办法

标题

使用 th.utext 而不是 th.text

【讨论】:

    【解决方案2】:

    您也可以使用 HTML5 标签来突出显示

    <p>test test test <mark>test</mark> today.</p>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 2019-12-08
      • 2018-08-29
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多