gpt4 book ai didi

javascript - 为什么 removeClass 不起作用?

转载 作者:行者123 更新时间:2023-11-30 16:52:27 25 4
gpt4 key购买 nike

我猜这真的很简单,但它一直让我发疯!下面是我的 jquery 代码,这是我的 Fiddle

$("#Headerhome").addClass('menuSelected').siblings().removeClass('menuSelected');
$('#Headerhome').children('#Site-icon-1').addClass('SiteIconHover').siblings().removeClass('SiteIconHover');

$("#HeaderSites").on('click', function () {

$(this).addClass('menuSelected').siblings().removeClass('menuSelected');
$(this).children('#Site-icon-2').addClass('SiteIconHover2').siblings().removeClass('SiteIconHover2');

});


$("#HeaderApps").on('click', function () {

$(this).addClass('menuSelected').siblings().removeClass('menuSelected');
$(this).children('#Site-icon-3').addClass('SiteIconHover3').siblings().removeClass('SiteIconHover3');

});

最佳答案

问题是您正在尝试寻找没有 sibling 的 div 的 sibling 。我更改了一些 CSS 和 JS。这应该可以满足您的要求。

JS:

 $("#Headerhome").addClass('menuSelected').siblings().removeClass('menuSelected');
$('#Headerhome').children('#Site-icon-1').addClass('SiteIconHover');

$("#Headerhome").on('click', function () {

$(this).addClass('menuSelected').siblings().removeClass('menuSelected');
$('div').removeClass('SiteIconHover');
$(this).children('#Site-icon-1').addClass('SiteIconHover');
});

$("#HeaderSites").on('click', function () {

$(this).addClass('menuSelected').siblings().removeClass('menuSelected');
$('div').removeClass('SiteIconHover');
$(this).children('#Site-icon-2').addClass('SiteIconHover');
});


$("#HeaderApps").on('click', function () {

$(this).addClass('menuSelected').siblings().removeClass('menuSelected');
$('div').removeClass('SiteIconHover');
$(this).children('#Site-icon-3').addClass('SiteIconHover');
});

CSS:已更新

#Site-icon-1 {

background-color:#000;
width:25px;
height:25px;
float:left;
margin-right:15px;
margin-top:12px;
margin-left:5px;
}

#Site-icon-1.SiteIconHover {
background-color:#00a9ff;
width: 25px;
height: 25px;
float: left;
margin-right: 15px;
margin-top: 12px;
margin-left: 5px;}


#Site-icon-2 {
background-color:#000;
width: 25px;
height: 25px;
float: left;
margin-right: 15px;
margin-top: 12px;
margin-left: 5px;
}

#Site-icon-2.SiteIconHover {
background-color:green;
width: 25px;
height: 25px;
float: left;
margin-right: 15px;
margin-top: 12px;
margin-left: 5px;}


#Site-icon-3 {
background-color:#000;
width: 25px;
height: 25px;
float: left;
margin-right: 15px;
margin-top: 12px;
margin-left: 5px;
}

#Site-icon-3.SiteIconHover {
background-color:red;
width: 25px;
height: 25px;
float: left;
margin-right: 15px;
margin-top: 12px;
margin-left: 5px;}


#Headerhome, #HeaderSites, #HeaderApps {

background: #254661;
font-weight: normal;
color: #eeeeee;
box-sizing: border-box;
outline: 0;
line-height:50px;
text-decoration: none;
font-size: 100%;
list-style: none;
width:240px;
height:50px;
display: block;
cursor: pointer;
margin-top: 2px;
margin-left:-20px;
margin-right:0px;
padding-left:20px;
font-family:'Segoe UI';

}

#HeaderSites.menuSelected {
border-left: 3px solid #26b3f7;
background: #18374f;
}

#Headerhome.menuSelected {
border-left: 3px solid #26b3f7;
background: #18374f;
}

#HeaderApps.menuSelected {
border-left: 3px solid #26b3f7;
background: #18374f;
}

演示: JSFiddle 已更新

关于javascript - 为什么 removeClass 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30307941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com