简单的ajax分页疑问,该如何处理

php中文网
发布: 2016-06-13 13:33:55
原创
900人浏览过

简单的ajax分页疑问

HTML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>ajax分页</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<script type="text/javascript">
function ajax() {
    var ajax = false;
    if(window.XMLHttpRequest) {
        ajax = new XMLHttpRequest();
    } else {
        ajax = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return ajax;
}
window.onload = function check(node) {
    var parameter = "page=" + node;
    var nokia = ajax();
    //alert(nokia);return;
    nokia.open('POST',"process.php",true);
    nokia.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    nokia.send(parameter);
    nokia.onreadystatechange = function () {
        if(nokia.readyState==4 && nokia.status==200) {
            document.getElementById('span1').innerHTML = this.responseText;
        }
    }
}
</script>

<style type="text/css">
</style>
</head>
    <body>
        <span id="span1"></span>
    </body>
</html>
登录后复制

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php
$page = isset($_POST['page'])?$_POST['page']+0:1;
if($page==0) {
$page =1;
}
$conn = mysql_connect('localhost','root','111111');
mysql_select_db('msg');
$sql = 'select count(*) from news';
$info = mysql_query($sql,$conn);
$msg = mysql_fetch_row($info);
$counts = $msg[0]; 
$perpage = 5; 
$pages = ceil($counts/$perpage); 
$start = $page - (5-1)/2;
$end = $page + (5-1)/2;
$start = $start<1?1:$start;
$end = ($start+5-1)>$pages?$pages:($start+5-1);
$end = $end>$pages?$pages:$end;
$start = ($end-5+1)<1?1:$end-5+1;
$link = '';
for($i=$start;$i<=$end;$i++) {
    if($i == $page) {
        $link .= $i;
        continue;
    }
    $link .= ' <a onclick="check(' . $i . ');" href="#">' . $i . '</a> ';
}
echo $link;
登录后复制


运行上面的html文件 可以显示 1 2 3 4 5 
可一单击超链接时,firefox的debug显示 check is not defined
不是已经innerHTML进来了么,而且奇怪的是那个$page如果不加零的话显示的object element,望朋友解答!!!

------解决方案--------------------

这样呢
function check(node) {
var parameter = "page=" + node;
var nokia = ajax();
//alert(nokia);return;
nokia.open('POST',"process.php",true);
nokia.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
nokia.send(parameter);
nokia.onreadystatechange = function () {
if(nokia.readyState==4 && nokia.status==200) {
document.getElementById('span1').innerHTML = this.responseText;
}
}
}
window.onload = check(node);


------解决方案--------------------
JScript code
window.onload = function ()
{
   check(1);
}
function check(node) {
    var parameter = "page=" + node;
    var nokia = ajax();
    //alert(nokia);return;
    nokia.open('POST',"process.php",true);
    nokia.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    nokia.send(parameter);
    nokia.onreadystatechange = function () {
        if(nokia.readyState==4 &amp;&amp; nokia.status==200) {
            document.getElementById('span1').innerHTML = this.responseText;
        }
    }
}
<br><font color="#e78608">------解决方案-------------------- <div class="clear"></div></font>
登录后复制
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号