jQuery - 从任何
可以说我有这样的列表...
<ul>
<li class="one">One</li>
<li class="two">Two</li>
<li class="three">Three</li>
</ul>
...我希望能够在 li 中切换类...
$( "li" ).click(function() {
$( "li" ).toggleClass( "active" );
});
..而且我希望除了被单击的那个之外的 li 永远不会显示为“.active”仍然保持“.active”li 中的toggleClass 功能
所以我想也许我想从除单击的那个li 之外的任何其他li 中删除Class。我不知道我怎么能做到这一点......
关于如何实现类似的事情有什么想法吗?我确信它相当简单,但我不知道,因为我对 jQuery 很陌生
已解决 - 如果有人想看它..我们就去: http://jsfiddle.net/6jm2s/17/ 还有这个... http://jsfiddle.net/6jm2s/18/
两个答案都很完美.. @乔达维约翰@hunter 谢谢你们...太糟糕了,我不能在两个答案上都打勾...但我会让猎人拥有它,因为他的代码更短..:)
Lets say I have list like this...
<ul>
<li class="one">One</li>
<li class="two">Two</li>
<li class="three">Three</li>
</ul>
...and I want to be able to toggle class in the li's...
$( "li" ).click(function() {
$( "li" ).toggleClass( "active" );
});
..also I want the li's except the one that was clicked to never show as '.active' while still maintaining the toggleClass ability in the '.active' li
So I was thinking maybe I want to removeClass from any other li's except the one what was clicked. I don't know how I could do that though...
Any ideas on how to achieve something like that? I'm sure its rather easy but I have no idea as I'm very new to jQuery
Solved - If someone wants to see it.. there we go:
http://jsfiddle.net/6jm2s/17/
also this one...
http://jsfiddle.net/6jm2s/18/
Both answers work perfectly.. @jondavidjohn & @hunter Thank you guys... too bad I can't put Checkmark on both answers... but I will let hunter have it as his code is shorter.. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保一旦某项处于活动状态,就始终处于活动状态:
仅关注尚未处于活动状态的
li
。能够停用:
工作示例:http://jsfiddle.net/6jm2s/18/
Make it so that once something is active, one will always be active:
focuses only on
li
's that aren't already active.To be able to deactivate:
working example: http://jsfiddle.net/6jm2s/18/