2 回答
TA贡献1803条经验 获得超3个赞
注意:-这是您可以根据选定元素切换(添加/删除)类的检测div的方法。
var div1 = document.getElementById("First");
div1.addEventListener("click", function (e) {
if (event.currentTarget.classList.contains('active')) {
event.currentTarget.classList.remove('active');
var element = document.getElementById("Second");
element.classList.add("active");
} else {
event.currentTarget.classList.add('active');
var element = document.getElementById("Second");
element.classList.remove("active");
}
});
var div2 = document.getElementById("Second");
div2.addEventListener("click", function (e) {
if (event.currentTarget.classList.contains('active')) {
event.currentTarget.classList.remove('active');
var element = document.getElementById("First");
element.classList.add("active");
} else {
event.currentTarget.classList.add('active');
var element = document.getElementById("First");
element.classList.remove("active");
}
});
.active{
color:red;
}
<div id"newNav">
<a class="navbar-item te" id="First" href="#Delivery">
<span class="icon"><i class="fas fa-shipping-fast"></i></span>
<span>Delivery</span>
</a>
<li class="navbar-item" id="Second"><a href="#Delivery"></a>
<span>Delivery2</span>
</li>
</div>
添加回答
举报