ajax页面中的前进与后台的实现

具体效果参考:http://www.zhangxinxu.com/study/201306/ajax-page-html5-history-api.html?

//以下是自己的部分理解<!DOCTYPE html><html><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <meta http-equiv="description" content="HTML5 history API与ajax分页 » 张鑫旭-鑫空间-鑫生活"/>    <meta name="description" content="张鑫旭web前端学习实例页面 HTML5 history API与ajax分页"/>    <meta name="keywords" content="ajax, 分页, html5, history, API"/>    <meta name="author" content="张鑫旭, zhangxixnu"/>    <title>HTML5 history API与ajax分页 » 张鑫旭-鑫空间-鑫生活</title>    <link rel="stylesheet" href="../css/demo.css" type="text/css"/>    <link rel="stylesheet" href="../css/hl.css" type="text/css"/>    <link rel="stylesheet" href="../css/zxx.lib.css" type="text/css"/> </head>

<body><div id="main">    <h1>HTML5 history API与ajax分页实例页面</h1>

    <div id="body" class="light">        <div id="content" class="show">            <h3>展示</h3>

            <div class="article_new"><a href="http://www.zhangxinxu.com/wordpress/?p=3432">回到相关文章 »</a></div>            <div class="demo">                <h4 class="tc f14">上海3月开盘项目汇总</h4>

                <div class="mt10 cho_box z">                    <b class="cho_line"></b>

                    <div class="fix">                        <!-- 左边的选菜项 -->                        <div class="cho_left">                            <div class="equal_height cho_equal_height"></div>                            <ul id="choMenu" class="rel cho_menu">                                <li><a href="ajax.php?area=pudong" class="cho_link">浦东区<span class="ml20">8</span></a>                                </li>                                <li><a href="ajax.php?area=baoshan" class="cho_link">宝山区<span class="ml20">7</span></a>                                </li>                                <li><a href="ajax.php?area=jiading" class="cho_link">嘉定区<span class="ml20">8</span></a>                                </li>                                <li><a href="ajax.php?area=qingpu" class="cho_link">青浦区<span class="ml20">3</span></a>                                </li>                                <li><a href="ajax.php?area=minhang" class="cho_link">闵行区<span class="ml20">4</span></a>                                </li>                                <li><a href="ajax.php?area=putuo" class="cho_link">普陀区<span class="ml20">2</span></a>                                </li>                                <li><a href="ajax.php?area=jinshan" class="cho_link">金山区<span class="ml20">3</span></a>                                </li>                                <li><a href="ajax.php?area=songjiang" class="cho_link">松江区<span                                        class="ml20">3</span></a></li>                                <li><a href="ajax.php?area=zhabei" class="cho_link">闸北区<span class="ml20">1</span></a>                                </li>                                <li><a href="ajax.php?area=fengxian" class="cho_link">奉贤区<span class="ml20">5</span></a>                                </li>                                <li><a href="ajax.php?area=huangpu" class="cho_link">黄浦区<span class="ml20">1</span></a>                                </li>                                <li><a href="ajax.php?area=changning" class="cho_link">长宁区<span                                        class="ml20">1</span></a></li>                                <li><a href="ajax.php?area=jingan" class="cho_link">静安区<span class="ml20">1</span></a>                                </li>                                <li><a href="ajax.php?area=zhoubian" class="cho_link">上海周边<span                                        class="ml20">1</span></a></li>                            </ul>                        </div>

                        <!-- 右侧主列表 -->                        <div class="cell">                            <ul class="fix cho_list_title">                                <li class="l pct20">楼盘名</li>                                <li class="l pct30">价格</li>                                <li class="cell">项目地址</li>                            </ul>                            <div id="listBox">                            </div>                        </div>                    </div>                </div>            </div>        </div>    </div></div>

<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><script>    var  eleMenuOn = null,         eleListBox = $("#listBox"),         tempList = $("#tempChoList").html(),         clMenuOn = "cho_link_on";

    String.prototype.temp = function (obj) {        return this.replace(/\$\w+\$/gi, function (matchs) {            var returns = obj[matchs.replace(/\$/g, "")];            return (returns + "") == "undefined" ? "" : returns;        });    };

    var eleMenus = $("#choMenu a").bind("click", function (event) {       // alert(2);        var query = this.href.split("?")[1];        if (history.pushState && query && !$(this).hasClass(clMenuOn)) {            eleMenuOn && eleMenuOn.removeClass("cho_link_on");            eleMenuOn = $(this).addClass("cho_link_on");            eleListBox.html(‘<div class="cho_loading"></div>‘);            $.ajax({                url: this.href,                dataType: "json",                success: function (data) {                    var html = ‘‘;                    if ($.isArray(data)) {                        $.each(data, function (i, obj) {                            html += tempList.temp(obj);                        });                    }                    eleListBox.html(html || ‘<div class="tc cr pt30">丫的没数据啊!</div>‘);                },                error: function () {                    eleListBox.html(‘<div class="tc cr pt30">数据获取失败!</div>‘);                }            });

            // history处理            var title = "上海3月开盘项目汇总-" + $(this).text().replace(/\d+$/, "");            document.title = title;            if (event && /\d/.test(event.button)) {                history.pushState({title: title}, title, location.href.split("?")[0] + "?" + query);            }        }        return false;    });    //该方法有多重用处 1. 页面初始载入时第一次调用,带参第二次调用  2.后退 前进是调用    var fnHashTrigger = function (target) {        var query = location.href.split("?")[1], eleTarget = target || null;        if (typeof query == "undefined") {     //首次载入

             //首次进入时            if (eleTarget = eleMenus.get(0)) {   //是"=" 先赋值,然后在判断eleTarget是否为空                history.replaceState(null, document.title, location.href.split("#")[0] + "?" + eleTarget.href.split("?")[1] +"#"+ location.hash);                fnHashTrigger(eleTarget);            }        } else {            eleMenus.each(function () {                if (eleTarget === null && this.href.split("?")[1] === query) {  ///eleTarget === null 在第二次调用时起作用,
                    eleTarget = this;                }            });

            if (!eleTarget) {                history.replaceState(null, document.title, location.href.split("?")[0]+"#"+location.hash);                fnHashTrigger();            } else {                $(eleTarget).trigger("click");            }        }    };    //初始载入时 history.pushState 不为空    if (history.pushState) {        window.addEventListener("popstate", function () {     //popstate 点击浏览器的前进后台 、或者调用history。go(back)时触发该事件            fnHashTrigger();        });        // 默认载入        //alert("00");        fnHashTrigger();         //初始载入时调用       // alert(3);    }</script>

</body></html>
时间: 2024-07-28 22:49:41

ajax页面中的前进与后台的实现的相关文章

jsp页面中使用javascript获取后台放在request或session中的值

在JSP页面中.常常使用javascript,可是要出javascript获取存储在request,session, application中的值.例如以下是获取request中的值: 如果后台中有: request.setAttribute("value", "123"); 在前台的javascript中有例如以下获取方式: 1 : var val =  "${value}"; 2: var val = "<%=request.

JS中直接从java后台获得对象的值(数组的值)

这里举得例子是:JS直接从后台Contorller中(SpringMVC中的model中)获得数值的值 Contorller 此处将 talentIntegralRecordsDay talentIntegralRecordsIntegral 两个数组用JSON.toJSONString()封装. @SuppressWarnings("deprecation") @RequestMapping("/integralParadise") public ModelAnd

wordpress——在插件后台管理页面中添加javascript和ajax

最近在开发一个wordpress插件,需要在插件的后台管理页面上,添加自己写的javascript文件,以达到一些功能. 查了好几天的文档和资料,终于实现了. 这里先介绍下wordpress后台页面添加javascript的过程,再介绍添加ajax的过程. 添加javascript 首先我们需要知道wordpress插件开发的框架,然后再介绍javascript添加的步骤. 添加插件设置页面 开发插件,总需要在管理后台添加自己的插件设置页面.插件设置子页面,在这些页面中,可以设置和保存插件的一些

C#后台程序与HTML页面中JS方法互调(功能类似于Ajax中的DWR)

此方法适用于 C#中嵌入WebBrowser(浏览器) 通过浏览器中加载的页面与C#的后台代码进行交互. 一.C#程序 1.在C#窗体中添加WebBrowser(浏览器),将页面的URL添加到浏览器中. 2.窗体代码添加 using System.Runtime.InteropServices;//和Html页面交互使用 在类的上一行添加 [ComVisible(true)]//和Html页面交互使用 在类的构造其中添加 this.webB.ObjectForScripting = this;

JSF页面中使用js函数回调后台bean方法并获取返回值的方法

由于primefaces在国内使用的并不是太多,因此,国内对jsf做系统.详细的介绍的资料很少,即使有一些资料,也仅仅是对国外资料的简单翻译或者是仅仅讲表面现象(皮毛而已),它们的语句甚至还是错误的,很可能会误导使用者. 相对来说,看国内的那些仅仅是翻译过来的文章或书籍不如直接看国外的官方文档或资料来的实在,在我讲述jsf页面中如何使用js调用后台bean方法之前,先给大家说几个国外的资料.在primefaces官方网站上,你可以搜索到几乎所有你需要的东西,primefaces官网为:http:

ajax中向页面中指定位置添加信息

$.ajax({  type : "POST",  beforeSend : function() {   showLoader("数据加载中...");// 展示等待效果  },  complete : function() {   //与后台通讯(查询是否存在有效保单)    getComPlugin(successCallback,"com.sinosoft.hna.lpcx.LPSQJYSrarch",'searchSQJY',dates

笔记-返回到前一个页面时显示前一个页面中ajax获取的数据

笔记第一部分:http://www.cnblogs.com/zczhangcui/p/6869219.html 在第一部分遇到的问题是,用ajax获取了一系列列表信息后,拼接好html后插入到了原有页面中,类似于一系列新闻列表,点击查看详情后再返回到这一页,会出现所有ajax获取并插入页面的结构都没有了,经过调研,决定尝试从history对象入手. 我使用了history.replaceState方法来改变当前页面的state,每次ajax获取信息后,将页面中希望保留的内容放入state中,当返

用juery的ajax方法调用aspx.cs页面中的webmethod方法示例

juery的ajax调用aspx.cs页面中的webmethod方法:首先在 aspx.cs文件里建一个公开的静态方法,然后加上WebMethod属性,具体实现如下,感兴趣的朋友可以参考下哈,希望对大家有所帮助 首先在 aspx.cs文件里建一个公开的静态方法,然后加上WebMethod属性. 如: [WebMethod] public static string GetUserName() { //...... } 如果要在这个方法里操作session,那还得将WebMethod的Enable

项目中Ajax调用ashx页面中的Function的实战

前台页面: 使用几个display=none的空间存储DropdownList中的值,点击Search Button后刷新页面再次给DropdownList赋值使用 <%@ Page Language="c#" CodeBehind="MallListCAM.aspx.cs" AutoEventWireup="True" Inherits="PRCSales_internal.Mall.MallListCAM" Enab