【发布时间】:2014-03-13 23:42:35
【问题描述】:
我相信这是我们主题中的分页部分,它在底部添加页码。出于 CSS 原因,虽然它输出 1、2、3 等,然后是 10、11、12。为了使事物正确对齐,它需要从一开始就为两位数,因此 01、02、03 等。
是否可以对这段代码这样做?
// generate pagination
if (option.generatePagination) {
// create unordered list
if (option.prependPagination) {
elem.prepend('<ul class='+ option.paginationClass +'></ul>');
} else {
elem.append('<ul class='+ option.paginationClass +'></ul>');
}
// for each slide create a list item and link
control.children().each(function(){
$('.' + option.paginationClass, elem).append('<li><a href="#'+ number +'">'+ (number+1) +'</a></li>');
number++;
});
} else {
// if pagination exists, add href w/ value of item number to links
$('.' + option.paginationClass + ' li a', elem).each(function(){
$(this).attr('href', '#' + number);
number++;
});
}
【问题讨论】:
-
您可以检查
if(number < 10)并使用number = '0'+number或其他东西,如果它是真的 -
即使添加零,您也永远无法使事物完全对齐(除非您使用的是等宽字体)。为什么不将 LI 的 CSS 设置为固定宽度,而不是使用额外的字符来修复格式问题?
-
@paul.abbott.wa.us 是的,你完全正确,这是要走的路,我想多了。我已经在 CSS 中做到了这一点,尽管我很想使用下面的答案(这在问题的上下文中也是正确的),因为我认为 01 实际上看起来也更好。谢谢。
标签: javascript jquery css pagination