在 javascript 代码中使用 Thymeleaf 模板引擎:

<script th:inline="javascript">
    $("#content").html(
        "<select name='status'>"+
        "   <option value=''>[[#{admin.common.choose}]]</option>"+
        "   <option value="+[[${status}]]+">[[#{'Order.Status.' + ${value}}]]</option>"+
        "</select>");
</script>

script 标签中的 th:inline="javascript" 属性值表示可以使用内联 js ,即可以在 js 代码中使用 [[]] 取值:

  var user = [[${user}]];

  //还可以使用下面这种形式(最后的表示默认值)
  var user = /*[[${user}]]*/ null;  

以上是在 Javascript 代码中使用 Thymeleaf 模板引擎的简单示例,但有时也会遇到不可解决或者说很难解决的问题。例如,如果要在 js 代码中输出一个段 html 代码,并且要在 html 代码中作循环操作,而 html 本身并没有提供这种功能的实现,这时要使用 Thymeleaf 的 th:each 属性,但是如何使用 th:each 在 js 中实现? 这时就不能像上面那样的使用字符串拼接内联 js 来实现了,因为 th:xx 是要作为标签的属性放在标签内部的,js 解析不了。解决的方案是将 html 代码放在一个使用 text/html 解析的 script 标签中,这样就会使用 html 的解析方式来解析这些代码,示例如下:

<script type="text/html" id="thymeleafTable">
    <table>
        <tr>
            <th th:text="#{Order.type}"></th>
            <td>
                <select name="type">
                    <option value="" th:text="#{admin.common.choose}"></option>
                    <option th:each="value : ${types}" th:value="${value}" th:attr="selected = ${value == type} ? 'selected' : ''" th:text="#{'Order.Type.' + ${value}}"></option>
                </select>
            </td>
        </tr>
    </table>
</script>  

然后在 js 代码中使用脚本的 id 来调用该脚本:

$("#content").html($("#thymeleafTable").html());  

text/csstext/htmltext/javascript 等属性值规定脚本的 MIME 类型,它表示浏览器的解释方式,例如,text/javascript 告诉浏览器按照 Javascript 来解析执行。

Thymeleaf 与 Javascript的更多相关文章

  1. thymeleaf 使用javascript定义数组报错

    js中免不了的要用的数组,一维的二维的三维的 但是当用到thymeleaf作为模版时候会有一些坑,导致数组不能用 org.thymeleaf.exceptions.TemplateProcessing ...

  2. Thymeleaf前后端传值 页面取值与js取值

    参考: Thymeleaf前后端传值 页面取值与js取值 Thymeleaf 与 Javascript Thymeleaf教程 (十二) 标签内,js中使用表达式 目的: 后端通过Model传值到前端 ...

  3. Thymeleaf使用说明

    Thymeleaf使用说明 javascript操作: a.<script type="text/javascript" th:inline="javascript ...

  4. SSM框架——thymeleaf学习总结

    本人关于thymeleaf的学习源自: https://www.bilibili.com/video/BV1qy4y117qi 1.thymeleaf的项目搭建 首先创建springboot项目,相关 ...

  5. thymeleaf的内联th:inline(在javascript访问model中的数据)

    thymeleaf模板引擎为前端数据的获取提供了较大的便利,在html标签内可通过th标签加${}表达式访问model里的对象数据.但如果不想通过th标签而是简单地访问model对象数据,或是想在ja ...

  6. Thymeleaf Javascript 取值

    <script th:inline="javascript"> var openid = /*[[${session.wxuser.openId}]]*/ </s ...

  7. spring boot(四):thymeleaf使用详解

    在上篇文章springboot(二):web综合开发中简单介绍了一下thymeleaf,这篇文章将更加全面详细的介绍thymeleaf的使用.thymeleaf 是新一代的模板引擎,在spring4. ...

  8. Thymeleaf

    1.在html顶部添加 <html xmlns:th="http://www.thymeleaf.org"> 2.url表达式 @{...} <link rel= ...

  9. Thymeleaf+Spring整合

    前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是SpringMvc框架. 注意Thymeleaf支持同Spring框架的3.和4.版本的集成,但是这两个版本的支持是封装在thym ...

随机推荐

  1. C Primer Plus_第6章_循环_编程练习

    1.题略 #include int main(void) { int i; char ch[26]; for (i = 97; i <= (97+25); i++) { ch[i-97] = i ...

  2. MAC 远程桌面链接 证书或链接无效

    RDC --> 首选项  -->  安全性 --> 勾选即使验证失败也要链接.   问题解决.

  3. Gym - 100917H

    题目链接 #include<cstdio> #include<cstring> #include<algorithm> #include<vector> ...

  4. sha256 C语言

    /** * \file sha2.h * * \brief SHA-224 and SHA-256 cryptographic hash function * * Copyright (C) 2006 ...

  5. myeclipse(2015)中创建简单的Maven项目的步骤(用于生成可执行jar文件)------》myeclipse2015

    利用MyEclipse的引导,可以很方便的创建简单的.用于生成可执行jar文件的Maven项目: 1.New -> Project... 选择 Maven Project, 点击Next > ...

  6. 【转】Caffe初试(六)激活层及参数

    在激活层中,对输入数据进行激活操作(实际上就是一种函数变换),是逐元素进行运算的.从bottom得到一个blob数据输入,运算后,从top输入一个blob数据.在运算过程中,没有改变数据的大小,即输入 ...

  7. 分布式追踪系统dapper

    http://www.cnblogs.com/LBSer/p/3390852.html 最近单位需要做自己的分布式监控系统,因此看了一些资料,其中就有google的分布式追踪系统dapper的论文:h ...

  8. chrome 不支持window.webkitNotifications.createNotification消息通知API了

    今天惊奇的发现,chrome22里已经不支持window.webkitNotifications.createHTMLNotification方法了: 但是,在chrome extension里还可以 ...

  9. Spring Web MVC 多viewResolver视图解析器解决方案

    viewResolver的定义如下: public interface ViewResolver { View resolveViewName(String viewName, Locale loca ...

  10. PCA数据降维

    Principal Component Analysis 算法优缺点: 优点:降低数据复杂性,识别最重要的多个特征 缺点:不一定需要,且可能损失有用的信息 适用数据类型:数值型数据 算法思想: 降维的 ...