【发布时间】:2019-07-07 03:51:08
【问题描述】:
我正在使用 Spring Boot、Bootstrap 4 和 Thymeleaf 开发一个 Web 应用程序。 我想显示一个引导警报,然后它会在几秒钟后自动隐藏(就像“成功”通知一样)。
我找到了很多解决方案,但都需要 JavaScript。当我尝试在我的项目中使用 JS 时,它不起作用。
我使用默认模板,通过layout:decorate="~{default}" 在其他视图中使用该模板。在这个默认模板中,我有 Bootstrap 链接和脚本:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />
...
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
如果将以下代码放入任何视图中,它什么也不做。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
...
</head>
<body class="text-center">
<div layout:fragment="content">
<div class="product-options">
<a id="myWish" href="javascript:;" class="btn btn-mini">Add to Wishlist </a>
<a href="" class="btn btn-mini"> Purchase </a>
</div>
<div class="alert alert-success" id="success-alert">
<button type="button" class="close" data-dismiss="alert">x</button>
<strong>Success! </strong> Product have added to your wishlist.
</div>
<script type="text/javascript">
$(document).ready(function () {
$("#success-alert").hide();
$("#myWish").click(function showAlert() {
$("#success-alert").fadeTo(2000, 500)
.slideUp(500, function () {
$("#successalert").slideUp(500);
});
});
});
</script>
...
</div>
</body>
注意:这只是一个检查 JS 不工作的例子。我从this post 那里得到它。
当我运行此代码时,会显示警报,但它永远不会消失。我尝试了很多脚本,但都没有工作。
可能是兼容性版本问题?
【问题讨论】:
-
在 thymeleaf 中写 instade of
-
非常感谢@Atanu,我必须添加 th:line 才能正确运行它。
标签: javascript jquery spring-boot bootstrap-4 thymeleaf