我动态创建了20个Divs。现在,我正在尝试向我的div添加前端分页,但是它不起作用。我正在点击this链接以向我的网页添加分页。下面是我创建div并应用分页的代码。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width">
<link type="text/css" href="pagination/simplePagination.css"/>
<script type="text/javascript" src="pagination/jquery.min.js"></script>
<script type="text/javascript" src="pagination/jquery.simplePagination.js"></script>
<style>
.containerdiv{
width: 50%;
position:absolute;
top: 50%;
left:50%;
transform: translate(-50%,-50%);
margin-top: 30px;
}
.image{
background-image: url('3332.jpg');
height:150px;
width: 100%;
}
.name{
text-align: center;
font-weight: bold;
margin-top:10px;
}
.description{
text-align: center;
margin-top:20px;
}
a{
text-decoration: none;
font-size: 20px;
margin-left: 20px;
}
a:visited{
color:blue;
}
a.current {
color: red;
font-size: 25px;
font-weight: bold;
}
#pagin li {
display: inline-block;
}
</style>
</head>
<body>
<h3 style="text-align: center;font-family: arial">Dynamically Created Div</h3>
<div id="main-content" class="slider" style="width: 50%;"></div>
<ul id="pagin"></ul>
<script>
var dataLength = 0;
var names = ['rajat','kunal','naman','divyank','krishna'];
var description = ['Rajat is a very good guy','Kunal is a very good guy','Naman is a very good guy','Divyank is a very good guy','Krishna is a very good guy'];
for(var i=0;i<5;i++){
dataLength++;
var mainContent = document.getElementById('main-content');
var container = document.createElement('div');
var imageDiv = document.createElement('div');
var vendorInfoDiv = document.createElement('div');
var vendorNameDiv = document.createElement('div');
var vendorDescriptionDiv = document.createElement('div');
vendorNameDiv.setAttribute('class','name');
vendorNameDiv.innerHTML = names[i];
vendorDescriptionDiv.setAttribute('class','description');
vendorDescriptionDiv.innerHTML = description[i];
vendorInfoDiv.appendChild(vendorNameDiv);
vendorInfoDiv.appendChild(vendorDescriptionDiv);
imageDiv.setAttribute("class","image");
imageDiv.setAttribute("style","background-image:url('3332.jpg')");
container.setAttribute("style","height:300px;width:250px;border: 1px solid red;background-image: (100%,100%);margin-left:12px;margin-top:10px;float:left");
container.appendChild(imageDiv);
container.appendChild(vendorInfoDiv);
mainContent.appendChild(container);
mainContent.setAttribute('class','card-container');
}
pageSize = 2;
var totalPaginate = Math.round(dataLength/pageSize);
for(var i=1;i<=totalPaginate;i++){
var ulist = document.getElementById('pagin');
var list = document.createElement('li');
var anchor = document.createElement('a');
anchor.setAttribute('href','#');
anchor.innerHTML = i;
list.appendChild(anchor);
ulist.appendChild(list);
}
showPage = function(page) {
$(".paginate").hide();
$(".paginate").each(function(n) {
console.log(n);
if (n >= pageSize * (page - 1) && n < pageSize * page)
$(this).show();
});
}
$("#pagin li a").click(function() {
$("#pagin li a").removeClass("current");
$(this).addClass("current");
showPage(parseInt($(this).text()))
});
showPage(1);
</script>
任何人都可以帮助我解决此问题,或者向我建议其他任何分页插件。
谢谢。
答案 0 :(得分:1)
只是忘记了插件,使用自定义代码将前端分页应用于动态创建的div元素。只需添加ID为“ main-content”的Div元素,如下所示即可添加无序列表。
<ul id="pagin"></ul>
并添加变量 var dataLength = 0; ,在此脚本开始,并像 dataLength ++ 一样在for循环中递增脚本;
在for循环结束后添加以下代码。
var pageSize = 2;
var totalPaginate = Math.round(dataLength/pageSize);
for(var i=1;i<=totalPaginate;i++){
var ulist = document.getElementById('pagin');
var list = document.createElement('li');
var anchor = document.createElement('a');
anchor.setAttribute('href','#');
anchor.innerHTML = i;
list.appendChild(anchor);
ulist.appendChild(list);
}
showPage = function(page) {
$(".paginate").hide();
$(".paginate").each(function(n) {
if (n >= pageSize * (page - 1) && n < pageSize * page)
$(this).show();
});
}
$("#pagin li a").click(function() {
$("#pagin li a").removeClass("current");
$(this).addClass("current");
showPage(parseInt($(this).text()))
});
showPage(1);
注意:您需要使用jquery来运行此代码。
这是最简单的前端分页代码。我已经编辑了您的代码,您只需复制并粘贴即可。