当前位置: 首页 > 知识库问答 >
问题:

jQuery在单击时添加类,在单击另一个类时删除

索和璧
2023-03-14

我有这样的标记

<div class="personal-menu-content">
    <ul>
         <li><a data-menu-item="lessons" class="personal-menu-item lessons" href="#">Lessons</a></li>
         <li><a data-menu-item="profile" class="personal-menu-item profile" href="#">Edit Profile</a></li>
         <li><a data-menu-item="library" class="personal-menu-item library" href="#">Your Library</a></li>
    </ul>
</div>

<div class="contents">
    <div id="lessons">
        <h2>Lessons text here</h2>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>
    </div>
    <div id="profile">
        <h2>profile text here</h2>
        <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.</p>
    </div>
    <div id="library">
        <h2>library text here</h2>
        <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words,</p>
    </div>
</div>

和像这样的JS

$('div#profile').show();
    $('body').on('click','a.personal-menu-item', function(e) {
        e.preventDefault();
        var selectedItem = $(this).attr('data-menu-item');
        if(selectedItem == 'lessons') {
            $('div#lessons').show();
            $('div#profile').hide();
            $('div#library').hide();
        }
        if(selectedItem == 'profile') {
            $('div#lessons').hide();
            $('div#profile').show();
            $('div#library').hide();
        }
        if(selectedItem == 'library') {
            $('div#lessons').hide();
            $('div#profile').hide();
            $('div#library').show();
        }
    });

所以在这里,我希望当我点击课程时,只有课程内容会显示出来,就像这样,当我点击配置文件和库时,只有配置文件和库会显示出来。在这里,它的工作很好,但我想知道如何添加一个类活动时,一个项目被点击在锚标记。假设我点击了lessons,那么一个活动类应该被添加到lessons锚标记中,当我点击了profile,那么活动类应该被从lessons锚标记中移除,它应该在profile锚标记中添加活动类。这是我的小提琴

共有1个答案

徐嘉勋
2023-03-14

您只需在单击处理程序中使用this引用

null

$('body').on('click.menu', 'a.personal-menu-item', function(e) {
  e.preventDefault();
  var selectedItem = $(this).attr('data-menu-item');
  var $selected = $('#' + selectedItem).show();
  $('.contents > div').not($selected).hide();
  $('.personal-menu-content .active').removeClass('active')
  $(this).addClass('active');
});
$('.personal-menu-content a[data-menu-item="profile"]').trigger('click.menu')
.contents > div {
  display: none;
}
.active {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="personal-menu-content">
  <ul>
    <li><a data-menu-item="lessons" class="personal-menu-item lessons" href="#">Lessons</a></li>
    <li><a data-menu-item="profile" class="personal-menu-item profile" href="#">Edit Profile</a></li>
    <li><a data-menu-item="library" class="personal-menu-item library" href="#">Your Library</a></li>
  </ul>
</div>

<div class="contents">
    <div id="lessons">
        <h2>Lessons text here</h2>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>
    </div>
    <div id="profile">
        <h2>profile text here</h2>
        <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.</p>
    </div>
    <div id="library">
        <h2>library text here</h2>
        <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words,</p>
    </div>
</div>
 类似资料:
  • 问题内容: 我正在尝试添加和删除被单击的li元素上的类。这是一个菜单,当我单击每个li项目时,我希望它获得该课程,而所有其他li项目都将其删除。因此,一次只有一个li项目上课。这就是我走了多远(见小提琴)。我不确定如何使“ about-link”从当前类开始,但是当单击其他li项之一时将其删除吗? 问题答案: 为什么不尝试这样的事情?

  • 在React.js中,我在中有页面链接菜单和3个切换按钮。 单击时,所有切换按钮都有层。 我想做的是。。。 当单击切换按钮时,必须删除其他切换按钮和图层。类别 当单击页面链接菜单(menu1、menu2、menu3)时,所有切换按钮和层都必须被删除 我需要什么来更改代码? 这就是我目前为止所尝试的。 请帮忙。 演示:https://codesandbox.io/s/49io4 header.js

  • 问题内容: 现在,我有一个div,它基本上是一个巨大的正方形,在div内,我还有另一个div,它是简单的文本,上面写着“ Upload File”,同时还有一个隐藏的input type = file元素。当用户按下div时,我要触发文件上传元素。到目前为止,我想出的代码是: 因此,我在CSS中将文件输入元素设置为。一旦他们在div id =“ test”中的任意位置单击,我想触发对文件上传元素的

  • 下拉框使用CSS和超文本标记语言/JS设计,使用从JS添加的名为“is-open”的类。一旦出现在指定的超文本标记语言div中,它将激活CSS来显示子菜单。 然而,有一个小问题,即一旦单击下拉框将不会消失,除非单击相同的菜单项。(当单击菜单内容div之外时,类不会取消切换) 作为一项基本功能,一旦用户不仅单击菜单,而且单击页面上的任何位置,该菜单都需要消失。 我目前的javascript如下所示:

  • 问题内容: 我一直在寻找如何执行此操作,但到目前为止我还没有找到任何相关的东西,:(我可以嵌套两个函数,但是我只是想知道这是否可行吗? 目前我的JS代码: 我想一键调用两个函数,如何在angularJS中做到这一点?我认为当您添加多个类时会像在CSS中那样直接…但是不是:( 问题答案: 您有2个选择: 创建包装这两个方法的第三个方法。这样做的好处是可以减少模板中的逻辑。 否则,如果要在ng-cli

  • 我正在开发一个发票系统,我想在每次单击添加新按钮时创建新字段。 每次都需要在下面的代码中添加字段。 字段需要显示在其各自的列下。 下面是我设置的按钮: 我知道这个网站上一定有答案,但我似乎找不到。 请注意,我是Java开发新手