我的屏幕上有自定义分页。 现在问题出在我做分页的时候
active
class
使用分页向前和向后移动。active
class
。我尝试使用javascript,但由于页面上有许多相同的值,因此会产生冲突。另外,如果我必须在7之后添加额外的li
点击下一个,那么是否可以在相同的功能中执行此操作?
请找到我的分页工作代码:
CSS:
/*Pagination CSS*/
ul.pagination {
display: inline-block;
padding: 0;
margin: 0;
}
ul.pagination li {display: inline;}
ul.pagination li a {
color: black;
float: left;
padding: 8px 16px;
text-decoration: none;
transition: background-color .3s;
border: 1px solid #ddd;
}
ul.pagination li a.active {
background-color: #4CAF50;
color: white;
border: 1px solid #4CAF50;
}
ul.pagination li a:hover:not(.active) {background-color: #ddd;}
div.center {text-align: center;}
/*Pagination CSS Ends here*/
HTML:
<div class="center">
<ul class="pagination">
<li><a href="#">«</a></li>
<li><a id="test1" href="#">1</a></li>
<li><a id="test2" class="active" href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">6</a></li>
<li><a href="#">7</a></li>
<li><a href="#" onclick="test()">»</a></li>
</ul>
</div>
使用Javascript:
function test() {
alert($('#myForm').find("input[value='" + 1 + "']").attr('id'));
document.getElementById("test2").className = "";
document.getElementById("test1").className = "active";
alert("test1");
}
答案 0 :(得分:2)
我提供完整的示例,防止箭头(prev,next)选择
$('ul.pagination').on('click', 'a', function() { // listen for click on pagination link
if($(this).hasClass('active')) return false;
var active_elm = $('ul.pagination a.active');
if(this.id == 'next'){
var _next = active_elm.parent().next().children('a');
if($(_next).attr('id') == 'next') {
// appending next button if reach end
var num = parseInt($('a.active').text())+1;
active_elm.removeClass('active');
$('<li><a class="active" href="#">'+num+'</a></li>').insertBefore($('#next').parent());
return;
}
_next.addClass('active');
}
else if(this.id == 'prev') {
var _prev = active_elm.parent().prev().children('a');
if($(_prev).attr('id') == 'prev') return false;
_prev.addClass('active');
} else {
$(this).addClass('active');
}
active_elm.removeClass('active');
});
ul.pagination {
display: inline-block;
padding: 0;
margin: 0;
}
ul.pagination li {display: inline;}
ul.pagination li a {
color: black;
float: left;
padding: 8px 16px;
text-decoration: none;
transition: background-color .3s;
border: 1px solid #ddd;
}
ul.pagination li a.active {
background-color: #4CAF50;
color: white;
border: 1px solid #4CAF50;
}
ul.pagination li a:hover:not(.active) {background-color: #ddd;}
div.center {text-align: center;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="center">
<ul class="pagination">
<li><a id="prev">«</a></li>
<li><a id="test1" href="#">1</a></li>
<li><a id="test2" class="active" href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">6</a></li>
<li><a href="#">7</a></li>
<li><a href="#" id="next">»</a></li>
</ul>
</div>
答案 1 :(得分:1)
$('ul.pagination li a').on('click', function () {
var $this = $(this),
$active = $('ul.pagination li a.active'),
$elem;
if ($this.is($active)) {
return;
}
switch ($this.attr('id')) {
case 'prev':
$elem = $active.parent().prev().children('a');
if ($this.is($elem)) {
return;
}
$elem.addClass('active');
break;
case 'next':
$elem = $active.parent().next().children('a');
if ($this.is($elem)) {
return;
}
$elem.addClass('active');
break;
default:
$this.addClass('active');
break;
}
$active.removeClass('active');
});
&#13;
/*Pagination CSS*/
ul.pagination {
display: inline-block;
padding: 0;
margin: 0;
}
ul.pagination li {display: inline;}
ul.pagination li a {
color: black;
float: left;
padding: 8px 16px;
text-decoration: none;
transition: background-color .3s;
border: 1px solid #ddd;
}
ul.pagination li a.active {
background-color: #4CAF50;
color: white;
border: 1px solid #4CAF50;
}
ul.pagination li a:hover:not(.active) {background-color: #ddd;}
div.center {text-align: center;}
/*Pagination CSS Ends here*/
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="center">
<ul class="pagination">
<li><a href="#" id="prev">«</a></li>
<li><a href="#">1</a></li>
<li><a class="active" href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">6</a></li>
<li><a href="#">7</a></li>
<li><a href="#"id="next">»</a></li>
</ul>
</div>
&#13;
答案 2 :(得分:0)
首先为li提供活动课程并更改css,以便在课程活跃的li下面的a
标签上应用样式
写下:
var index = $(".pagination > li.active").index();
$(".pagination > li.active").removeClass("active")
$(".pagination > li").eq(index-1).addClass("active")
当您点击特殊分页项时,请执行以下操作:
$(".pagination > li").on("click",function(){
$(".pagination > li.active").removeClass("active");
$(this).addClass("active");
})
答案 3 :(得分:0)
首先,我建议在test
函数中使用一个可以通知next / previous的参数。然后,您应该搜索current element
,然后向相应的方向移动。
我还添加了选择任何页面的处理。
$('.pagination li').on('click', function() {
var $this = $(this)
if ($this.data('action')) {
$this.removeClass('active');
if (index > 0) {
$this.closest('li').next().find('a').addClass('active');
} else {
$this.closest('li').prev().find('a').addClass('active');
}
} else {
$('.pagination li a').removeClass('active');
$this.find('a').addClass('active')
}
})
&#13;
ul.pagination {
display: inline-block;
padding: 0;
margin: 0;
}
ul.pagination li {
display: inline;
}
ul.pagination li a {
color: black;
float: left;
padding: 8px 16px;
text-decoration: none;
transition: background-color .3s;
border: 1px solid #ddd;
}
ul.pagination li a.active {
background-color: #4CAF50;
color: white;
border: 1px solid #4CAF50;
}
ul.pagination li a:hover:not(.active) {
background-color: #ddd;
}
div.center {
text-align: center;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="center">
<ul class="pagination">
<li><a href="#" onclick='test(-1)'>«</a>
</li>
<li><a href="#">1</a>
</li>
<li><a class="active" href="#">2</a>
</li>
<li><a href="#">3</a>
</li>
<li><a href="#">4</a>
</li>
<li><a href="#">5</a>
</li>
<li><a href="#">6</a>
</li>
<li><a href="#">7</a>
</li>
<li><a href="#" onclick="test(1)">»</a>
</li>
</ul>
</div>
&#13;