简体   繁体   English

jquery / javascript:如何从父元素的所有子元素中删除“活动”类?

[英]jquery/javascript: How do I remove the 'active' class from all the children of a parent element?

I have a few lists, something like so: 我有一些清单,像这样:

<div id="menu" class="divclass">
    <ul id="firstlist">
        <li><a href='#'>...</a></li>
        <li><a href='#'>...</a></li>
        <li><a href='#'>...</a></li>
    </ul>
    <ul id="secondlist">
        <li><a href='#'>...</a></li>
        <li><a href='#'>...</a></li>
        <li><a href='#'>...</a></li>
    </ul>
    <!-- etc. - similar unordered lists follow. -->
</div>

When one of the <a> elements is clicked, I want to clear all the <li> elements of its list of the 'active' class. 单击<a>元素之一时,我想清除其'active'类列表中的所有<li>元素。 I've been fiddling around with .parent() , .children() , and .removeClass('active') but I think I'm totally missing something because I can't get it to work. 我一直在摆弄.parent() .children().removeClass('active')但我认为我完全错过了一些东西,因为我无法使用它。 Can anyone give me a hand? 有人可以帮我吗? This is the beginning of the script: 这是脚本的开头:

$(document).ready(function() {
    $(".divclass ul li a").click(function() { ... }}

where .divclass is the class of the div that contains the unordered lists. 其中.divclass是包含无序列表的div的类。

Remove all 移除所有

$(".divclass ul li a").click(function() {
    $(this).closest('.divclass').find('li').removeClass('active');
}};

Remove only form the same list 仅删除同一列表

$(".divclass ul li a").click(function() {
    $(this).closest('ul').find('li').removeClass('active');
}};

If you also want to add the class active to the li parent of the a clicked: 如果您还想将活动类添加到单击的li父级中:

$(".divclass ul li a").click(function() {
    $(this).parent().addClass('active').siblings().removeClass('active');
}};

You can use: 您可以使用:

 $("#menu a").click(function() {
    //to remove class active from li elements
    $(this).closest('ul').find('.active').removeClass('active');


    //to remove li with active class
    $(this).closest('ul').find('.active').remove();
 });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Javascript从父级中的所有子级中删除活动类 - How to remove the active class from all children in a parent using Javascript 如何在单击元素时切换类,但使用 JQuery 从所有其他元素中删除相同的类? - How do I toggle a class on click of an element but remove the same class from all other elements using JQuery? 如何在事件侦听器中将父节点与其所有子节点相关联? jQuery / JavaScript - How do I associate a parent node with all of its children in an event listener? jQuery / JavaScript 如何在jQuery中将子元素移动到新的父元素? - How do I move children elements to a new parent element in jQuery? 如何使用jQuery从元素中删除所有子级? - How to remove all children from an element using jQuery? 删除父元素和所有子元素 - Remove parent element and all children 如何在纯 JavaScript 中向元素添加和删除活动类 - How can I add and remove an active class to an element in pure JavaScript 如何从父元素中删除所有子元素(但 2 个)并将 append 删除到新的子元素? - How to remove all children (but 2) from a parent element and append it to a new child element? 如果类在另一个元素上处于/不活动状态但保留子元素,则删除/添加父元素 - Remove/Add parent element if class is/isn't active on another element but keep the children Javascript-如何删除所有活动类,除非被单击的元素已经具有活动类 - Javascript - How to remove all active classes unless the element being clicked on already has a active class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM