jQuery實現簡單導航欄的樣式切換

style css樣式部分:css

ul{
margin: 0 auto;
height: 50px;
background-color: #369;
}html

ul>li{
text-decoration: none;
display: inline-block;
height: 50px;
line-height: 50px;
color: #fff;
padding: 0 15px;
border-left: 1px solid #365;this

}
.active,ul>li:hover{
background-color: #fff;
color: #369;
cursor: pointer;
}

html部分:htm

<ul>
<li>首頁</li><li>關於咱們</li><li>產品中心</li><li>新聞中心</li><li>關於咱們</li>
</ul>

方法一:https://www.jkys120.com/article/95498/rem

$li = $('ul>li');
// console.log($li);
$li.bind('click',function(){
$this=$(this);
$this.addClass('active');
$this.siblings().removeClass('active');
console.log('111');
});https://www.jkys120.com/article/95498/產品


方法二:(原生Js)io

for(let i = 0 ;i < navs.length; i++){
navs[i].onclick = function(e){
let activedNav = this.parentNode.querySelector('.active');
if(activedNav == this){
return;
}
activedNav.className = '';
this.className = 'active';
}
}console

相關文章
相關標籤/搜索