Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 【原创】分页高级教程:结合JQUERY做AJAX分页

【原创】分页高级教程:结合JQUERY做AJAX分页

Jun 23, 2016 pm 01:56 PM
ajax jquery Pagination Original Tutorial

本文主要结合JQUERY做无刷新分页。代码基本上和前两篇文章差不多,稍稍有所变动。本文中的翻页链接用JS编写。先上代码:
page4.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>结合jquery做ajax分页</title><style>body{ font-size:12px; font-family:Verdana, Arial, Helvetica, sans-serif}.page A{ padding:3px 5px; float:left; border:solid #CCCCCC 1px; text-decoration:none; font-size:12px; margin-left:1px; font-family:Verdana;color:#000000;}.page A.currentpage{ color:#FF0000; border: solid red 1px;}.page A:hover{ background-color:#CCCCCC;}</style><script language="javascript" src="jquery-1.7.1.min.js"></script></head><body><? //PHP分页实例//分页尺寸$page_size=10;$conn=mysql_connect("localhost","root","root") or die(mysql_error());mysql_select_db("xinyang");//计算总行数$total_records=mysql_num_rows(mysql_query("SELECT id from product"));//总页数$total_page=ceil($total_records/$page_size); $query=mysql_query("select * from product limit 0,$page_size")  or die(mysql_error());?><div class="recordlist"><ul><?while ($rs=mysql_fetch_array($query)){?>	 <li><?=$rs["id"]?>-<?=$rs["ename"]?></li><?}?></ul></div><div class='page'></div></body></html><script language="javascript">var total_page=<?=$total_page?>;var url="page5.php?page";//当前链接,格式如下,例如:?aa=1&page  ,somepage.php?action=1&cat=1&page$().ready(function(){	$(".page").html(pagination(1))		page_link();})function page_link(){	$(".page A").click(function(){		var page=parseInt($(this).attr("page")) 		$(".recordlist").load($(this).attr("href"),"",function(){$(".page").html(pagination(page));page_link();})		return false;	})}function pagination(current_page){ 	//翻页链接开始	current_page = current_page<=0 ? 1 : current_page;	current_page = current_page>total_page?total_page:current_page;	var page_link="";	if (total_page>1)	{		page_link="<a>一共"+total_page+"页</a>";		page_link+="<a>当前第"+current_page+"页</a>";		page_link+="<a href="+url+"=1 page=1>首页</a>";		if (current_page>1){			//页码大于1的时候,显示上一页翻页链接			var pre_page=current_page-1;			page_link+="<a  href='"+url+"="+pre_page+"' page="+pre_page+"><<</a>";		}		//翻页列表		//步进分页,翻页列表的数字始终只显示9个或者自定义的个数,本例定义只显示9个,当前页左右各显示4个页码		if (total_page>9)		{			if (current_page>4)			{				var from=current_page-4;				var to=current_page+4;				if (to>total_page)				{					var from=total_page-8;					var to=total_page;				}				 			}			else			{				var from=1;				var to=9;			}		}		else		{			var from=1;			var to=total_page;		}		for (var i=from;i<=to;i++)		{			if (i==current_page)			{				//高亮当前页页码				page_link+="<a href='"+url+"="+i+"' class='currentpage' page="+i+">"+i+"</a>";			}			else			{				page_link+="<a href='"+url+"="+i+"' page="+i+"  page="+i+">"+i+"</a>";			}		}		//页码小于总页数的时候显示下一页翻页链接		var next_page=current_page+1;		if(next_page<total_page)		{			page_link+="<a  href='"+url+"="+next_page+"' page="+next_page+">>></a>";		}		page_link+="<a  href='"+url+"="+total_page+"' page="+total_page+">最后一页</a>";	}	return page_link;}</script>
Copy after login


page5.php

<? $page=$_GET["page"]+0;$page= $page<=0 ? 1 : $page; //分页尺寸$page_size=10;$conn=mysql_connect("localhost","root","root") or die(mysql_error());mysql_select_db("xinyang");$offset=($page-1)*$page_size;$query=mysql_query("select * from product limit $offset,$page_size")  or die(mysql_error());?> <ul><?while ($rs=mysql_fetch_array($query)){?>	 <li><?=$rs["id"]?>-<?=$rs["ename"]?></li><?}?></ul>
Copy after login


回复讨论(解决方案)

本文的核心部分就是JS部分的 page_link()函数,当用户点击翻页链接之后,脚本将对div.recordlist绑定一个load事件,这个主要用来加载下一页的内容。
看看jquery手册上对load事件的说明:载入远程 HTML 文件代码并插入至 DOM 中。

如果我们仅仅只这样:
$(".recordlist").load($(this).attr("href")) 这样是可以翻页,但是问题出现了,发现翻页链接不能发生变化,,,所以需要在回调函数里面,重新初始化翻页链接,因此在回调函数总加入$(".page").html(pagination(page));这里初始化翻页之后,需要对.page A重新绑定click事件,因此在回调函数里面加上page_link().

由于是点击的的对象是.page A,点击之后,会发生跳转,因此必须终止跳转,所以必须加上return false来终止。。。



暂且说这么多,欢迎拍砖~

你将分页链接和内容做成一个模板  每次请求的时候把整个模板load这个模板就行

减少一些js操作

学习了…………

几点意见.
1 pagination 函数用意是拼接输出字符串,建议封装为模板.
2 此分页,没有考虑带上查询条件。

page5.php有什么 用吗?

好用,非常感谢

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Tutorial on how to use Dewu Tutorial on how to use Dewu Mar 21, 2024 pm 01:40 PM

Dewu APP is currently a very popular brand shopping software, but most users do not know how to use the functions in Dewu APP. The most detailed usage tutorial guide is compiled below. Next is the Dewuduo that the editor brings to users. A summary of function usage tutorials. Interested users can come and take a look! Tutorial on how to use Dewu [2024-03-20] How to use Dewu installment purchase [2024-03-20] How to obtain Dewu coupons [2024-03-20] How to find Dewu manual customer service [2024-03-20] How to check the pickup code of Dewu [2024-03-20] Where to find Dewu purchase [2024-03-20] How to open Dewu VIP [2024-03-20] How to apply for return or exchange of Dewu

PHP and Ajax: Building an autocomplete suggestion engine PHP and Ajax: Building an autocomplete suggestion engine Jun 02, 2024 pm 08:39 PM

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

In summer, you must try shooting a rainbow In summer, you must try shooting a rainbow Jul 21, 2024 pm 05:16 PM

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

What software is photoshopcs5? -photoshopcs5 usage tutorial What software is photoshopcs5? -photoshopcs5 usage tutorial Mar 19, 2024 am 09:04 AM

PhotoshopCS is the abbreviation of Photoshop Creative Suite. It is a software produced by Adobe and is widely used in graphic design and image processing. As a novice learning PS, let me explain to you today what software photoshopcs5 is and how to use photoshopcs5. 1. What software is photoshop cs5? Adobe Photoshop CS5 Extended is ideal for professionals in film, video and multimedia fields, graphic and web designers who use 3D and animation, and professionals in engineering and scientific fields. Render a 3D image and merge it into a 2D composite image. Edit videos easily

Tutorial on how to turn off the payment sound on WeChat Tutorial on how to turn off the payment sound on WeChat Mar 26, 2024 am 08:30 AM

1. First open WeChat. 2. Click [+] in the upper right corner. 3. Click the QR code to collect payment. 4. Click the three small dots in the upper right corner. 5. Click to close the voice reminder for payment arrival.

PHP Tutorial: How to convert int type to string PHP Tutorial: How to convert int type to string Mar 27, 2024 pm 06:03 PM

PHP Tutorial: How to Convert Int Type to String In PHP, converting integer data to string is a common operation. This tutorial will introduce how to use PHP's built-in functions to convert the int type to a string, while providing specific code examples. Use cast: In PHP, you can use cast to convert integer data into a string. This method is very simple. You only need to add (string) before the integer data to convert it into a string. Below is a simple sample code

A simple tutorial on converting full-width English letters to half-width letters A simple tutorial on converting full-width English letters to half-width letters Mar 25, 2024 pm 09:21 PM

When using a computer to input English, sometimes we encounter the difference between full-width English letters and half-width English letters. Full-width English letters refer to the characters input by pressing the Shift key and the English letter key combination when the input method is Chinese mode. They occupy a full-width character width. Half-width English letters refer to characters input directly when the input method is English mode, and they occupy half a character width. In some cases, we may need to convert full-width English letters to half-width letters. Here is a simple tutorial: First, open a text editor or any

Experts teach you! The Correct Way to Cut Long Pictures on Huawei Mobile Phones Experts teach you! The Correct Way to Cut Long Pictures on Huawei Mobile Phones Mar 22, 2024 pm 12:21 PM

With the continuous development of smart phones, the functions of mobile phones have become more and more powerful, among which the function of taking long pictures has become one of the important functions used by many users in daily life. Long screenshots can help users save a long web page, conversation record or picture at one time for easy viewing and sharing. Among many mobile phone brands, Huawei mobile phones are also one of the brands highly respected by users, and their function of cropping long pictures is also highly praised. This article will introduce you to the correct method of taking long pictures on Huawei mobile phones, as well as some expert tips to help you make better use of Huawei mobile phones.

See all articles