[英]JS pagination first page <a> active class
I have this JS pagination code that works almost like I want it to, except from that I want the selected-page
class to be added to the first page number <a>
before it is clicked? 我有这个JS分页代码,其工作方式几乎与我想要的一样,除了我希望将
selected-page
类在单击之前添加到首页编号<a>
? Now it is only added after the current page is clicked. 现在,仅在单击当前页面后才添加它。
(function($) {
$(function() {
$.widget("zpd.paging", {
options: {
limit: 5,
rowDisplayStyle: 'block',
activePage: 0,
rows: []
},
_create: function() {
var rows = $("tbody", this.element).children();
this.options.rows = rows;
this.options.rowDisplayStyle = rows.css('display');
var nav = this._getNavBar();
this.element.after(nav);
this.showPage(0);
},
_getNavBar: function() {
var rows = this.options.rows;
var nav = $('<div>', {class: 'paging-nav'});
for (var i = 0; i < Math.ceil(rows.length / this.options.limit); i++) {
this._on($('<a>', {
href: '#',
text: (i + 1),
"data-page": (i)
}).appendTo(nav),
{click: "pageClickHandler"});
}
//create previous link
this._on($('<a>', {
href: '#',
text: '<<',
"data-direction": -1
}).prependTo(nav),
{click: "pageStepHandler"});
//create next link
this._on($('<a>', {
href: '#',
text: '>>',
"data-direction": +1
}).appendTo(nav),
{click: "pageStepHandler"});
return nav;
},
showPage: function(pageNum) {
var num = pageNum * 1; //it has to be numeric
this.options.activePage = num;
var rows = this.options.rows;
var limit = this.options.limit;
for (var i = 0; i < rows.length; i++) {
if (i >= limit * num && i < limit * (num + 1)) {
$(rows[i]).css('display', this.options.rowDisplayStyle);
} else {
$(rows[i]).css('display', 'none');
}
}
},
pageClickHandler: function(event) {
event.preventDefault();
$(event.target).siblings().attr('class', "");
$(event.target).attr('class', "selected-page");
var pageNum = $(event.target).attr('data-page');
this.showPage(pageNum);
},
pageStepHandler: function(event) {
event.preventDefault();
//get the direction and ensure it's numeric
var dir = $(event.target).attr('data-direction') * 1;
var pageNum = this.options.activePage + dir;
//if we're in limit, trigger the requested pages link
if (pageNum >= 0 && pageNum < this.options.rows.length) {
$("a[data-page=" + pageNum + "]", $(event.target).parent()).click();
}
}
});
});
})(jQuery);
Without your HTML, I couldn't tell you a solution that works out of the box. 没有您的HTML,我无法告诉您开箱即用的解决方案。 However, you can write the jQuery:
但是,您可以编写jQuery:
$("a:first-child").addClass("selected-page");
or another selector like it. 或其他类似的选择器。
i think that you can do this when you create the pages links 我认为您可以在创建页面链接时执行此操作
$('<a>', {
href: '#',
text: (i + 1),
"data-page": (i),
"class": i == 0 ? "selected-page" : "" <----- ADD THIS
})
Here in your _create
function you are showing page number 0. 在
_create
函数中,您将显示第0页。
this.showPage(0);
You can either replace this with a programmatic click on your first <a>
, 您可以用编程方式单击第一个
<a>
来代替它,
$("a[data-page='0']").click();
or simply add class name to it, instead of clicking: 或直接向其中添加类名,而不是单击:
$("a[data-page='0']").addClass("selected-page");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.