Jquery 将类添加到特定元素
我的问题是因为我对jQuery几乎一无所知,我在Bootstrap()中使用一个脚本进行分页,我在这行Jquery 将类添加到特定元素,jquery,pagination,element,addclass,Jquery,Pagination,Element,Addclass,我的问题是因为我对jQuery几乎一无所知,我在Bootstrap()中使用一个脚本进行分页,我在这行$('.pager.page_link:first').addClass('active')中看到了这一点活动类被添加到之前的锚点,并且必须将其添加到li元素 我试着把$('.pager**li**.page_link:first').addClass('active')但它不起作用 非常感谢 编辑:代码: var listElement = $('#pageStuff'); var perPa
$('.pager.page_link:first').addClass('active')中看到了这一点代码>活动类被添加到之前的锚点,并且必须将其添加到li元素
我试着把$('.pager**li**.page_link:first').addClass('active')代码>但它不起作用
非常感谢
编辑:代码:
var listElement = $('#pageStuff');
var perPage = 2;
var numItems = listElement.children().size();
var numPages = Math.ceil(numItems/perPage);
$('.pager').data("curr",0);
var curr = 0;
**while(numPages > curr){
$('<li><a href="#" class="page_link">'+(curr+1)+'</a></li>').appendTo('.pager');
curr++;
}
$('.pager .page_link:first').addClass('active');**
listElement.children().css('display', 'none');
listElement.children().slice(0, perPage).css('display', 'block');
$('.pager li a').click(function(){
var clickedPage = $(this).html().valueOf() - 1;
goTo(clickedPage,perPage);
});
function previous(){
var goToPage = parseInt($('.pager').data("curr")) - 1;
if($('.active').prev('.page_link').length==true){
goTo(goToPage);
}
}
function next(){
goToPage = parseInt($('.pager').data("curr")) + 1;
if($('.active_page').next('.page_link').length==true){
goTo(goToPage);
}
}
function goTo(page){
var startAt = page * perPage,
endOn = startAt + perPage;
listElement.children().css('display','none').slice(startAt, endOn).css('display','block');
$('.pager').attr("curr",page);
}
var-listlement=$('#pageStuff');
每页var=2;
var numItems=liselement.children().size();
var numPages=Math.ceil(numItems/每页);
$('.pager')。数据(“当前”,0);
var curr=0;
**而(数值>当前值){
$('')。附加到('.pager');
curr++;
}
$('.pager.page_link:first').addClass('active')**
css('display','none');
liselement.children().slice(0,每页).css('display','block');
$('.pager li a')。单击(函数(){
var clickedPage=$(this.html().valueOf()-1;
转到(单击页面,每页);
});
函数previous(){
var goToPage=parseInt($('.pager').data(“curr”))-1;
if($('.active').prev('.page_link').length==true){
后藤(后藤);
}
}
函数next(){
goToPage=parseInt($('.pager').data(“curr”))+1;
if($('.active_page')。next('.page_link')。length==true){
后藤(后藤);
}
}
功能转到(第页){
var startAt=每页*页,
endOn=startAt+每页;
css('display','none').slice(startAt,endOn.css('display','block');
$('.pager').attr(“curr”,第页);
}
由于li
是.page\u link
的父项,因此您可以使用最接近的(“li”)
或.parent()
编辑
$('.pager li a').click(function(){
var clickedPage = $(this).html().valueOf() - 1;
$(".active").removeClass("active");
$(this).closest("li").addClass("active");
goTo(clickedPage,perPage);
});
这个小片段将active
类添加到单击的
:
//when the anchor tag is clicked
$('a.page_link').click(function(){
$('li').each(function(){
//remove all li with class active (this is to remove the previous active)
$(this).removeClass('active');
});
//add active to the parent of the clicked a tag
$(this).parent().addClass('active');
});
第二个不起作用..但第一个起作用。但是现在第一个li元素始终处于活动状态,如果我单击第二个元素,这不会变为活动状态,我不知道为什么,谢谢!
$('.pager li a').click(function(){
var clickedPage = $(this).html().valueOf() - 1;
$(".active").removeClass("active");
$(this).closest("li").addClass("active");
goTo(clickedPage,perPage);
});
//when the anchor tag is clicked
$('a.page_link').click(function(){
$('li').each(function(){
//remove all li with class active (this is to remove the previous active)
$(this).removeClass('active');
});
//add active to the parent of the clicked a tag
$(this).parent().addClass('active');
});