我有这样的标记
<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锚标记中添加活动类。这是我的小提琴
您只需在单击处理程序中使用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>