我正在使用 Thymeleaf 在輸入錯誤憑據時在我的登錄表單上顯示錯誤訊息:
<body>
<div class="container">
<form class="form" id="login" th:action="@{/login}" method="post">
<h1 class="form__title">Login</h1>
<div th:if="${param.error}" class="form__message form__message-error">
invalid username or password
</div>
</div>
</body>
由于此錯誤訊息 div 僅在輸入錯誤憑據時出現,因此我的表單更改了其布局 - 輸入欄位向下移動。我不要這個。我希望表單以任何一種方式看起來都一樣,只有訊息應該被寫入或洗掉。有誰知道如何實作這一目標?
uj5u.com熱心網友回復:
th:if只會<div>在error請求引數存在時添加。要執行您想要的操作,您應該使用 CSS 來擁有一個空的 div 或顯示一些文本,th:classappend用于選擇要使用的 CSS 類。
就像是:
<body>
<div class="container">
<form class="form" id="login" th:action="@{/login}" method="post">
<h1 class="form__title">Login</h1>
<div th:classappend="${param.error}?'show':'hide'" class="form__message form__message-error">
invalid username or password
</div>
</div>
</body>
隨著.show并.hide在你的CSS定義的類。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/367299.html
