[英]How to apply a class to a list item using JS or jQuery without grabbing by id and keeping active?
So I'm using a CMS that doesn't allow me to apply an onclick on the a tag or use an onclick. 因此,我使用的CMS不允许我在代码上应用onclick或使用onclick。 So it basically outputs in code like the following:
因此,它基本上以如下代码输出:
<ul class="ul-class" id="ul-id">
<li class="nav-items"><a href="1">Something1</a></li>
<li class="nav-items"><a href="2">Something2</a></li>
<li class="nav-items"><a href="3">Something3</a></li>
<li class="nav-items"><a href="4">Something4</a></li>
<li class="nav-items"><a href="5">Something5</a></li>
</ul>
What I need to be able to do is apply a class showing an underline on the link that's been clicked that then leaves if another link has been clicked. 我需要做的是在单击的链接上应用显示下划线的类,如果单击了另一个链接,则该类留下。 Unfortunately I cannot get the class to stay present with the click.
不幸的是,我无法让课程保持在点击状态。
I've tried this: JS Onclick Add Class 我已经尝试过: JS Onclick添加类
But the OP was using an onclick to latch onto. 但是OP正在使用onclick进行锁定。
What I've ended up with is this: 我最终得到的是:
$(".nav-items").click(function (e) {
$(this).addClass("nav-active").siblings().removeClass("nav-active");
});
Then for the CSS I have: 然后对于CSS我有:
a.nav-active{
border-bottom: 5px solid green;
padding-bottom: 10px;
}
The border pops up but leaves immediately. 边界弹出但立即离开。 Is there something wrong with the code or do I need to put something else in to make the border permanent unless clicked elsewhere?
代码是否有问题,或者是否需要放置其他东西才能使边框永久化,除非单击其他位置?
You can do something like: 您可以执行以下操作:
Note: You have to use .nav-active
instead of a.nav-active
because your .nav-active
is <li>
and an <a>
注意:您必须使用
.nav-active
而不是a.nav-active
因为您的.nav-active
是<li>
和<a>
$(function() { $(".nav-items").click(function(e) { e.preventDefault(); /* To prevent redirect of <a> */ $(".nav-items").removeClass("nav-active"); /* Remove the class to all nav-items */ $(this).addClass("nav-active"); /* Add the class to clicked nav-items*/ }); });
.nav-active { border-bottom: 5px solid green; padding-bottom: 10px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="ul-class" id="ul-id"> <li class="nav-items"><a href="1">Something1</a></li> <li class="nav-items"><a href="2">Something2</a></li> <li class="nav-items"><a href="3">Something3</a></li> <li class="nav-items"><a href="4">Something4</a></li> <li class="nav-items"><a href="5">Something5</a></li> </ul>
I had to reverse your CSS (to .nav-active a
instead of a.nav-active
) and add e.preventDefault();
我不得不反转您的CSS(改为
.nav-active a
而不是a.nav-active
)并添加e.preventDefault();
to the jQuery, but otherwise it seems to be working. 到jQuery,但否则似乎可以正常工作。 It wouldn't hurt to remove the class first before adding it, so change
在添加类之前先删除该类不会有什么坏处,因此请进行更改
$(this).addClass("nav-active").siblings().removeClass("nav-active");
to 至
$(this).siblings().removeClass("nav-active");
$(this).addClass("nav-active");
A possible approach with plain JavaScript 使用纯JavaScript的可能方法
var myItems = Array.from(document.querySelectorAll(".nav-items")); // make array from NodeList
myItems.forEach(navItem => navItem.addEventListener("click", event => {
myItems.forEach(maybeSelectedBefore => maybeSelectedBefore.classList.remove("nav-active"));
event.target.classList.toggle("nav-active", true);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.