`
edicky
  • 浏览: 38595 次
  • 来自: ...
社区版块
存档分类
最新评论

使用jQuery实现局部刷新

阅读更多

关键就在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就不会去更新内容。解决方法也很简单,加一个随机数就好了。最终版本:

 

 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"
type="text/javascript"></script>
<script type="text/javascript" language="javascript">
$(function(){
	setInterval(function(){$("#cnt").load(location.href+"?rnd="+(Math.ceil(Math.random()*1000))+" #cnt");},3000);
});
</script>
<body>
<div id="cnt">
<?php echo date('y/m/d H:i:s').' <span style="color:red">memory usage:</span> '.memory_get_usage()?>
</div>
</body>
 也可以获取同域下的其他文件,并在特定部分显示。如果要跨域获取内容的话,比较方便的方法是调用同域的php文件,然后在该php文件里通过file_get_contents之类的函数去远程抓取,说白了就是充当采集器的角色。
在线演示
分享到:
评论
3 楼 javaeye_mao 2009-08-24  
第三方地方
2 楼 javaeye_mao 2009-08-24  
<!--#include file="conn.asp"-->
<%
response.charset="gb2312"
Dim ComeUrl,cUrl
ComeUrl = LCase(Trim(Request.ServerVariables("HTTP_REFERER")))
If ComeUrl="" Then
   Response.Write "<br><p align=center><font color='red'>对不起,为了系统安全,不允许直接输入地址访问本系统页面。</font></p>"
   Response.End
Else
   cUrl = Trim("http://" & Request.ServerVariables("SERVER_NAME"))
   If Mid(ComeUrl,Len(cUrl)+1,1) = ":" then
    cUrl = cUrl & ":" & Request.ServerVariables("SERVER_PORT")
   End If
   cUrl = LCase(cUrl & Request.ServerVariables("SCRIPT_NAME"))
   If LCase(Left(ComeUrl,Instrrev(ComeUrl,"/"))) <> LCase(Left(cUrl,Instrrev(cUrl,"/"))) Then
    Response.Write "<br><p align=center><font color='red'>对不起,为了系统安全,不允许从外部链接地址访问本系统页面。</font></p>"
    Response.End
   End If
End If
1 楼 javaeye_mao 2009-08-24  
<!--#include file="conn.asp"-->
<%
response.charset="gb2312"
Dim ComeUrl,cUrl
ComeUrl = LCase(Trim(Request.ServerVariables("HTTP_REFERER")))
If ComeUrl="" Then
   Response.Write "<br><p align=center><font color='red'>对不起,为了系统安全,不允许直接输入地址访问本系统页面。</font></p>"
   Response.End
Else
   cUrl = Trim("http://" & Request.ServerVariables("SERVER_NAME"))
   If Mid(ComeUrl,Len(cUrl)+1,1) = ":" then
    cUrl = cUrl & ":" & Request.ServerVariables("SERVER_PORT")
   End If
   cUrl = LCase(cUrl & Request.ServerVariables("SCRIPT_NAME"))
   If LCase(Left(ComeUrl,Instrrev(ComeUrl,"/"))) <> LCase(Left(cUrl,Instrrev(cUrl,"/"))) Then
    Response.Write "<br><p align=center><font color='red'>对不起,为了系统安全,不允许从外部链接地址访问本系统页面。</font></p>"
    Response.End
   End If
End If
Global site tag (gtag.js) - Google Analytics