SpringBoot----thymeleaf的點擊後改變樣式32

側邊欄希望能做到點擊之後改變顏色或者樣式,讓用戶知道自己所在的目錄

這裏只是簡單的例子,具體可以查看usingthymeleaf的pdf文檔

第一步:給引入的側邊欄傳入固定的參數,當打開這個頁面的時候固定的向側邊欄傳入參數

<div th:replace="~{commons/bar :: #sidebar(activeUri='emps')}"></div>

這裏向id爲sidebar的片段傳入activeUri。可以通過逗號分隔來添加多個變量名


第二步:在側邊欄的片段部分的class標籤做判斷

<li class="nav-item">
                <a
                   th:class="${activeUri=='main.html'?'nav-link active':'nav-link'}"
                   th:href="@{/main.html}">
                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-home">
                        <path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path>
                        <polyline points="9 22 9 12 15 12 15 22"></polyline>
                    </svg>
                    Dashboard <span class="sr-only">(current)</span>
                </a>
            </li>

這裏用來(?a:b)三元表達式做判斷,通過參數選擇樣式

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章