首页 > PHP > 使用jQuery实现局部刷新

使用jQuery实现局部刷新

2009年05月11号
查看评论 发表评论 2,703次浏览

实现
真的是太简单了,让我忍不住再次感慨一下jQuery的宗旨:Write Less Do More。看一下实现代码

01.<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"
02.type="text/javascript"></script>
03.<script type="text/javascript" language="javascript">
04.setInterval(function(){$("#cnt").load(location.href+" #cnt");},3000);
05.</script>
06.<body>
07.<div id="cnt">
08.<?php echo date('y/m/d H:i:s').' <span style="color:red">memory usage:</span> '.memory_get_usage()?>
09.</div>
10.</body>

关键就在load方法,看一下官方解释Load HTML from a remote file and inject it into the DOM.
A GET request will be performed by default - but if you pass in any extra parameters then a POST will occur.

In jQuery 1.2 you can now specify a jQuery selector in the URL. Doing so will filter the incoming HTML document, only injecting the elements that match the selector. The syntax looks something like “url #some > selector”. Default selector “body>*” always applies. See the examples for more information.
远程获取文件,然后插入到指定的节点中,默认为get方法,如果有其他的参数,则会使用post方法。jQuery1.2之后的版本可以指定选择器。而我们知道jQuery的选择器之强大是出了名的,所以我们可以获取当前页某一节点的内容,然后插入到该节点中,就实现了局部刷新的效果。

如果你运行上面的这段代码,会发现IE下并没有效果,内容没有更新。这是因为IE有缓存,如果数据源的URL和当前的URL一致,那么IE就不会去更新内容。解决方法也很简单,加一个随机数就好了。最终版本:

01.<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js”
02.type=”text/javascript”></script>
03.<script type=”text/javascript” language=”javascript”>
04.$(function(){
05.
setInterval(function(){$(”#cnt”).load(location.href+”?rnd=”+(Math.ceil(Math.random()*1000))+” #cnt”);},3000);
06.});
07.</script>
08.<body>
09.<div id=”cnt”>
10.<?php echo date(’y/m/d H:i:s’).’ <span style=”color:red”>memory usage:</span> ‘.memory_get_usage()?>
11.</div>
12.</body>

也可以获取同域下的其他文件,并在特定部分显示。如果要跨域获取内容的话,比较方便的方法是调用同域的php文件,然后在该php文件里通过file_get_contents之类的函数去远程抓取,说白了就是充当采集器的角色。

类别PHP 标签
  1. windows7下载
    发表于 2010年01月18号 09时24分53秒 | 1楼

    已阅

  2. 淘宝购物返现
    发表于 2010年01月18号 13时58分45秒 | 2楼

    网站做的很漂亮哦!~文章写的也很好,我是初学者,刚刚开始做网站,还请给予指导。我会经常关注你的,希望你的关注下我的网站好吗?

  3. 淘宝购物返现
    发表于 2010年01月22号 13时51分47秒 | 3楼

    第二次来了,留个言~祝博主博客人气高高!!

  4. 人性本色
    发表于 2010年01月23号 15时05分46秒 | 4楼

    看了不回不礼貌啊

  5. 淘宝购物返现
    发表于 2010年01月27号 18时45分15秒 | 5楼

    页面干净,网站整洁!淘宝购物返现站长前来支持!祝博主人气高高!

  6. 济南网站优化
    发表于 2010年01月28号 16时00分39秒 | 6楼

    济南网站优化(www.shandong-seo.cn)在寻找链接的时候找到了贵博客,呵呵,博主写的不错,加油哈,期待回访,多多交流。

  7. тв онлайн дискавери
    发表于 2010年05月12号 20时20分18秒 | 7楼

    ок загляну еще

  8. заказать футболку россия
    发表于 2010年05月18号 15时08分33秒 | 8楼

    ок измените шрифты плохо видно

  9. nike air max
    发表于 2010年06月13号 18时20分08秒 | 9楼

    Many times I’ve read you and thought that I agree to many of your views and ways.

  10. установка циркуляционного насоса
    发表于 2010年06月17号 16时53分34秒 | 10楼

    ок важнецки

  11. Cheap timberland boots
    发表于 2010年06月22号 16时55分20秒 | 11楼

    I love your imagination

  12. lacoste Shoes
    发表于 2010年06月24号 08时33分47秒 | 12楼

    I definitely agree,is like the saying

  13. fendi handbags on sale
    发表于 2010年06月27号 16时58分57秒 | 13楼

    What a beautiful creation

  14. kobe shoes
    发表于 2010年06月30号 14时56分25秒 | 14楼

    Super post.

  15. cheap nba basketball shoes
    发表于 2010年07月08号 07时22分34秒 | 15楼

    How perfect! Beautiful advice!

评论页数: