当我单击具有“右箭头”或“左箭头”类的 div 时,我想向对象添加一个类。在http://jsfiddle.net/上它是否完美运行。但在我的网站上它不起作用:(
谁能帮我?
jQuery:
var currentPage = 0;
var lastPage = ($(".post-preview > div").length)-1;
currentPage = $('.post-preview > div.show').attr('id');
$('.news > .right').stop().click(function() {
if (currentPage == lastPage) {
$('.post-preview > div#' + lastPage).hide();
$('.post-preview > div#' + lastPage).removeClass('show');
$('.post-preview > div#0').fadeIn(300);
$('.post-preview > div#0').addClass('show');
currentPage = 0;
} else {
$('.post-preview > div#'+currentPage).hide();
$('.post-preview > div#'+currentPage).removeClass('show');
currentPage++;
var nextPage = currentPage;
currentPage--;
$('.post-preview > div#'+nextPage).fadeIn(300);
$('.post-preview > div#'+nextPage).addClass('show');
currentPage = nextPage;
}
});
$('.news > .left').stop().click(function() {
if (currentPage == 0) {
$('.post-preview > div#0').hide();
$('.post-preview > div#0').removeClass('show');
$('.post-preview > div#' + lastPage).fadeIn(300);
$('.post-preview > div#' + lastPage).addClass('show');
currentPage = lastPage;
} else {
$('.post-preview > div#'+currentPage).hide();
$('.rpost-preview > div#'+currentPage).removeClass('show');
currentPage--;
var nextPage = currentPage;
currentPage++;
$('.post-preview > div#'+nextPage).fadeIn(300);
$('.post-preview > div#'+nextPage).addClass('show');
currentPage = nextPage;
}
});
显示类的 CSS:
.news > .post-preview > div.show { display: inline-block !important; }
我的 HTML 代码:
<div class="news">
<div class="arrow left"></div>
<div class="post-preview">
<div id="0" class='show'></div>
<div id="1"></div>
<div id="2"></div>
</div>
<div class="arrow right"></div>
</div>