Ajax load Instance in jquery

Source: Internet
Author: User

For example, the following iplaysoft in this section of the site.

You can also first look at what I do a little

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>ajax load</title>
<style type= "Text/css" >
*{margin:0px;padding:0px;font-size:14px;font-family:verdana, Arial, Helvetica, Sans-serif;
A{text-decoration:none;}
. c{clear:both;height:0px;}
h3{width:450px;margin:20px auto 0px auto;font-size:16px;text-align:center;}
Small{font-size:12px;color: #999999;}
. nav{width:450px;margin:0px Auto;height:auto;}
. Nav Li{float:left;margin:5px;list-style:none;}
. Nav Li A{background:url (btn.jpg) No-repeat;width:12px;height:12px;display:block;}
. Nav Li A:hover,.nav li a#now{background-position:-12px;
#hello {width:450px;border:1px #CCCCCC solid;margin:10px auto 0px Auto;}
#hello a{display:block;padding:7px;border-top: #CCCCCC 1px dashed;color: #666666;}
#hello A#first{border:none;}
#hello a:hover{color: #000000; background: #F7F7F7;}
</style>
<script type= "Text/javascript" src= "Http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" ></ Script>

<body>

<div id= "Ajaxload" >

<script language= "javascript" type= "Text/javascript" >
$ ("#ajaxload"). Load ("test.php");
function NextPage (page) {
$ ("#ajaxload"). Load ("test.php", {' page ':p age});
}
</script>
</div>

</body>


Click each tab to convert to see the different information content. Here to see how it is achieved, in fact, simply need two files on it, One is to show you that it can be HTML or PHP Dynamic Web page format (as a framework for the following index.html), and the other must be a CGI format file, because it is used to get triggers and make the information change?

First, you introduce the jquery framework

The code is as follows Copy Code

<script language= "javascript" type= "Text/javascript" src= "Js/jquery.min.js" ></script>

Then in that index page put a div as a container, for the selection of containers you can do according to their own needs, nor is it necessary to do with Div, and give it an ID number as a hook. Then write the script directly.

The code is as follows Copy Code

<div id= "Ajaxload" >
<script language= "javascript" type= "Text/javascript" >
$ ("#ajaxload"). Load ("test.php");//default let this div load test.php content
</script>
</div>

Let's take a look at what we can load on the test page and write a hello or something on the test page.

Sure enough this method is can drop, then we further expand, to achieve our function. Let's refer to the template above to make it, of course, we can play their own imagination to achieve other effects. Ajaxload the layout of this div is probably as follows, of course, because the entire div content is from the test page, so put the following frame on the test page.

The code is as follows Copy Code

<div id= "Hello" >
<a href= "#" >message</a>
</div>
<ul class= "NAV" >
<li><a href= "#" >1</a></li>
</ul>
<div class= "C" ></div>

And then write the PHP code to implement the Hello Div to extract the database information, nav that for the number of pages (PHP code here will not put out, more, mainly to achieve reading database information, as well as according to the page pagination display data). For pagination features write it yourself here, Paperen I have written so far.

  code is as follows copy code

    $each =5;//Define the number of data per page
    $page =isset ($ _post[' page '])? Intval ($_post[' page '): 1;//check for page variables and filter
    $db =new SQL ()//Create an object (the SQL class is not posted here, Because it is long and involves some sensitive information)
    $total = $db->getcount ("SELECT count (*) from ' table '");//Get Total data
     $pagenum = ($total% $each ==0)? $total/$each: (int) ($total/$each) +1;//and determine the number of pages
    $page = ($page > $pagenum | | $page <=0)? 1: $page ; Further correctly filter the page variable
    $offset = ($page-1) * $each//Get the data location of the query
    $sql = "SELECT ID , title from ' table ' ORDER by ID Desc LIMIT $offset, $each ';//sql statement

Then use a For loop to display the number of pages, because you already know the total number of pages $pagenum.

The code is as follows Copy Code
<ul class= "NAV" >
<?php
for ($i =1; $i <= $pagenum; $i + +) {
?>
<li><a href= "javascript:void (0);" ></a></li>
<?php
}
?>
</ul>
<div class= "C" ></div>

Then write a CSS to beautify the page. The effect is as follows.?

The last step, the realization of the interactive function, is that we click on the orange dot, the information above will change, but also the point of comparison, I do not know what the children see here there is no idea, Paperen I think I'll see if I can get the elements of the test page on this index page and receive its action, I'm <li><a href= "javascript:void (0);" ></a></li> plus a Click event

Just test and see if it's okay.

Sure enough, Xi hee this is good to do. Then in this hyperlink click event with a function to handle, in the index page plus some script.

The code is as follows Copy Code

<li><a href= "javascript:void (0);" onclick= "NextPage (<?= $i?>)" ></a></li>
<script language= "javascript" type= "Text/javascript" >
$ ("#ajaxload"). Load ("test.php");
function NextPage (page) {
$ ("#ajaxload"). Load ("test.php", {' page ':p age});
}
</script>

Note that the Load event is a POST method for sending data, which is why you use $_post to receive page variables on the test page.

The

really is a great front-desk effect, we can go back to try, in fact, see so many of the simplest is $ ("#ajaxload"). Load ("test.php", {' page ':p age}); it can be solved, compared to post,get feel more convenient ah.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.