JQuery 中的分页问题
我需要能够使用 JQuery 解析 XML 文件,同时显示 3 个帖子,并具有链接到其余帖子的分页。
在下面的代码中,我正在解析从slashdot 下载的本地 XML 文件。该代码显示了正确数量的帖子并创建了分页链接,但是当您单击分页链接时,它们由于某种原因不起作用。我仍然是一个 JQuery n00b,所以我在找出问题所在时遇到了问题。好像JQuery没有一个很好的调试工具?
ps 您可以下载 http://slashdot.org/slashdot.xml 到本地,以便测试代码,如果你想要的话。
这是代码
<html>
<head>
<script type="text/javascript" src="jquery-1.6.4.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$.ajax({
type: "GET",
url: "slashdot.xml",
dataType: "xml",
success: parseXml
});
});
function parseXml(xml)
{
//find every story
var count = 0;
$(xml).find("story").each(function()
{
count++;
var title = $(this).find('title').text()
var url = $(this).find('url').text()
var fullLink = '<li><a href="'+url+'">' + title + '</a></li>';
//document.write('<a href="'+url+'">' + title + '</a><br/>');
$("#content").append(fullLink);
});
var show_per_page = 3;
var number_of_items = count;
var number_of_pages = Math.ceil(number_of_items/show_per_page);
$('#current_page').val(0);
$('#show_per_page').val(show_per_page);
var navigation_html = '<a class="previous_link" href="javascript:previous();">Prev</a> ';
var current_link = 0;
while(number_of_pages > current_link){
navigation_html += '<a class="page_link" href="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'">'+ (current_link + 1) +'</a>';
current_link++;
}
navigation_html += '<a class="next_link" href="javascript:next();"> Next</a>';
$('#page_navigation').html(navigation_html);
$('#page_navigation .page_link:first').addClass('active_page');
$('#content').children().css('display', 'none');
$('#content').children().slice(0, show_per_page).css('display', 'block');
function previous(){
new_page = parseInt($('#current_page').val()) - 1;
//if there is an item before the current active link run the function
if($('.active_page').prev('.page_link').length==true){
go_to_page(new_page);
}
}
function next(){
new_page = parseInt($('#current_page').val()) + 1;
//if there is an item after the current active link run the function
if($('.active_page').next('.page_link').length==true){
go_to_page(new_page);
}
}
function go_to_page(page_num){
//get the number of items shown per page
var show_per_page = parseInt($('#show_per_page').val());
//get the element number where to start the slice from
start_from = page_num * show_per_page;
//get the element number where to end the slice
end_on = start_from + show_per_page;
//hide all children elements of content div, get specific items and show them
$('#content').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');
/*get the page link that has longdesc attribute of the current page and add active_page class to it
and remove that class from previously active page link*/
$('.page_link[longdesc=' + page_num +']').addClass('active_page').siblings('.active_page').removeClass('active_page');
//update the current page input field
$('#current_page').val(page_num);
}
//$("#content").append('count:' + count);
}
</script>
</head>
<body>
<!-- we will add our HTML content here -->
<input type="hidden" id="current_page"></input>
<input type="hidden" id="show_per_page"></input>
<div id="content">
</div>
<div id="page_navigation"></div>
</body>
</html>
I need to be able to parse an XML file with JQuery, show 3 posts at the time and have pagination that links to the rest of the posts.
Below in the code, I am parsing a local XML file that I have downloaded from slashdot. The code displays the right amount of posts and creates the links to paginate but when you click the pagination links, they do not work for some reason. I am still a JQuery n00b so I have problems figuring out what is wrong. Seems like JQuery does not have a really good debugging tool?
p.s. You can download http://slashdot.org/slashdot.xml to your local so you can test the code if you want.
here is the code
<html>
<head>
<script type="text/javascript" src="jquery-1.6.4.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$.ajax({
type: "GET",
url: "slashdot.xml",
dataType: "xml",
success: parseXml
});
});
function parseXml(xml)
{
//find every story
var count = 0;
$(xml).find("story").each(function()
{
count++;
var title = $(this).find('title').text()
var url = $(this).find('url').text()
var fullLink = '<li><a href="'+url+'">' + title + '</a></li>';
//document.write('<a href="'+url+'">' + title + '</a><br/>');
$("#content").append(fullLink);
});
var show_per_page = 3;
var number_of_items = count;
var number_of_pages = Math.ceil(number_of_items/show_per_page);
$('#current_page').val(0);
$('#show_per_page').val(show_per_page);
var navigation_html = '<a class="previous_link" href="javascript:previous();">Prev</a> ';
var current_link = 0;
while(number_of_pages > current_link){
navigation_html += '<a class="page_link" href="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'">'+ (current_link + 1) +'</a>';
current_link++;
}
navigation_html += '<a class="next_link" href="javascript:next();"> Next</a>';
$('#page_navigation').html(navigation_html);
$('#page_navigation .page_link:first').addClass('active_page');
$('#content').children().css('display', 'none');
$('#content').children().slice(0, show_per_page).css('display', 'block');
function previous(){
new_page = parseInt($('#current_page').val()) - 1;
//if there is an item before the current active link run the function
if($('.active_page').prev('.page_link').length==true){
go_to_page(new_page);
}
}
function next(){
new_page = parseInt($('#current_page').val()) + 1;
//if there is an item after the current active link run the function
if($('.active_page').next('.page_link').length==true){
go_to_page(new_page);
}
}
function go_to_page(page_num){
//get the number of items shown per page
var show_per_page = parseInt($('#show_per_page').val());
//get the element number where to start the slice from
start_from = page_num * show_per_page;
//get the element number where to end the slice
end_on = start_from + show_per_page;
//hide all children elements of content div, get specific items and show them
$('#content').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');
/*get the page link that has longdesc attribute of the current page and add active_page class to it
and remove that class from previously active page link*/
$('.page_link[longdesc=' + page_num +']').addClass('active_page').siblings('.active_page').removeClass('active_page');
//update the current page input field
$('#current_page').val(page_num);
}
//$("#content").append('count:' + count);
}
</script>
</head>
<body>
<!-- we will add our HTML content here -->
<input type="hidden" id="current_page"></input>
<input type="hidden" id="show_per_page"></input>
<div id="content">
</div>
<div id="page_navigation"></div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,你的html无效。输入标签是自闭合的,
li
项需要位于列表ul
或ol
内,而不是div
元素。其次,您的点击事件未得到处理,因为
go_to_page
、next
和previous
不在全局范围内。您应该创建这些元素并附加点击处理程序。另一个提示,重组上一页和下一页功能,只需单击上一页或下一页页码。这样,
go_to_page
中的this
始终指向分页链接。JSFiddle 删除了 AJAX 部分。
First, your html is invalid. Input tags are self-closing and
li
items need to be inside of a listul
orol
not adiv
element.Second, your click events aren't getting handled because
go_to_page
,next
andprevious
are not in the global scope. You should create those elements and attach click handlers.Another tip, restructure the prev and next functions to just click on the previous or next page number. That way,
this
ingo_to_page
always points to the paging link.JSFiddle with the AJAX part removed.