简体   繁体   English

Thymeleaf:在Javascript中使用带有'th:href'的链接

[英]Thymeleaf: Use a link with 'th:href' in Javascript

I have the following link: 我有以下链接:

<a th:href="@{/linkToPage}">...</a>

which works. 哪个有效。 But now I have a workaround where I have to "create" this link in Javascript: 但是现在我有了一种解决方法,必须在Javascript中“创建”此链接:

$('#div').html(
    '<a th:href="@{/linkToPage}">...</a>'
);

but now the link does not work anymore. 但现在该链接不再起作用。 Is there a way to get link work and call the corresponding 'get' method in the controller? 有没有一种方法可以使链接工作并在控制器中调用相应的“ get”方法? Thank you very much. 非常感谢你。

You can achieve it using Thymeleag script inlining. 您可以使用Thymeleag脚本内联来实现。 Try to add th:inline="javascript" to the script tag and initizialize a link variable to use it in your script as follows. 尝试将th:inline="javascript"script标签,并初始化一个link变量以在脚本中使用它,如下所示。 You can find out more about Thymeleaf script inlining here . 您可以在此处找到有关Thymeleaf脚本内联的更多信息。

<script type="text/javascript" th:inline="javascript">
/*<![CDATA[*/

    var link = /*[[@{/linkToPage}]]*/'';

    $('#div').html(
        '<a href="' + link + '">...</a>'
    );

 /*]]>*/
 <script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM