thymeleaf js source url

thymeleaf js source url

本文关键字:url source js thymeleaf      更新时间:2023-09-26

我试图开发一个web应用程序与thyymeleaf,我已经创建了一个html页面,使用外部JavaScript文件来改变图像URL。但是标准语法URL:

document.getElementById("im1").src="images/img1.jpg" ;

不起作用。JavaScript代码中的其他所有内容都可以正常工作。我应该使用哪种URL语法?

试试这样写:

<script th:inline="javascript">
    /*<![CDATA[*/
        var context = [[@{/}]];
    /*]]>*/
    document.getElementById("im1").src=context + 'images/img1.jpg' ;
</script>

也许你的代码没有到达上下文中的图像。调试或使用控制台查看从:context + 'images/img1.jpg'生成的URL。然后尝试通过浏览器访问它。正常情况下它应该显示图像