嘗試從所有.clickMe
鏈接移除.active
類,那麼.active
類添加到當前點擊鏈接:
$(document).ready(function() {
$('.clickMeInfo').hide();
$('.clickMe').click(function() {
$('.clickMeInfo').fadeOut('fast');
$(this).next('.clickMeInfo').fadeIn('fasst');
$('.clickMe').removeClass('active');
$(this).addClass('active');
});
});
你可以也做一些優化,因爲相同的選擇器將被反覆使用:
$(document).ready(function() {
//cache the elements instead of selecting them multiple times
var $clickMe = $('.clickMe'),
$clickMeInfo = $('.clickMeInfo');
$clickMeInfo.hide();
$clickMe.click(function() {
//find the index of the clicked element
var $this = $(this),
this_index = $clickMe.index($this);
//fade-out all the info elements, then select only the clicked index and fade it in
$clickMeInfo.fadeOut('fast').filter(':eq(' + this_index + ')').fadeIn('fast');
$clickMe.removeClass('active');
$this.addClass('active');
});
});
以下是對上述優化的jsfiddle的更新:http://jsfiddle.net/nZFUP/4/