使用Thymeleaf设置导航栏选中后高亮

1、首先引入JS,用#thisNav接收后台标记的字段,获取该页面的id,根据class="a-first"取到需高亮导航的id(此id需要根据后台字段获取th:data-id="${nav.id}"),逻辑:若该页面id与选中的导航id一致thisValue == item.dataset.id,则在class="a-first"这个class中追加一个selected样式

window.onload = function(){
    var thisValue = document.querySelector('#thisNav').value
    var navs = document.querySelectorAll('.a-first')
    console.log(navs)
	for(let item of navs){
		if(thisValue == item.dataset.id){
		   item.classList.add('selected')
		}
	}
}
$('#large-input').focus(function(){
	        $('#pageNav').css({
				display:'none'
			})
			$(this).css({
				width:'96%'
			})
			$('#large-form').css({
				width:'100%'
			})
	    });
	$('#large-input').blur(function(){
	        $('#pageNav').css({
	        	display:'block'
	        })
			$(this).css({
				width:'100px'
			})
			$('#large-form').css({
				width:'auto'
			})
	    });

2、对需要高亮的导航设置class和data-id,class用于获取该高亮部分的id以及追加高亮的样式

 3、设置选中后的当前页面导航高亮的样式,追加了一个样式,既加入了selected后(上述JS判断是否追加)就使用该样式

<style>
    .a-first.selected{
        color: #f7bd00 !important
    }
</style>

4、在页面中引入标记的字段,用来获取本页面的id

 <input type="hidden" id="thisNav" th:value="${commonContent.oldParentId }" />

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值