【问题标题】:Using Jquery to refresh all of a page but a single image使用 Jquery 刷新所有页面但单个图像
【发布时间】:2015-03-02 14:05:33
【问题描述】:

我已经阅读了如何使用 jquery 刷新单个 div,但是我有一个主要基于表格和更新元素的网页。我目前有一个单独的脚本来每 1.5 秒刷新一次网页,但是我的 Web_Main 文件中有一个静态图像,每次页面刷新时都会闪烁,所以我想在我的 Web_Run 文件中使用 jquery 来排除该图像页面刷新。我已经在我的目录文件夹中包含了 jquery-1.11.2.min.js 文件并有标题来引用它,但我不熟悉 jquery 并且不知道如何为除我的图像刷新之外的所有内容制作 div,如以及我应该把它放在 Web_Main 脚本还是 Web_Run 脚本中。

我的 Web_Main 设置为:

<div class ="refreshpage"> 


<!-- Rest of page content needing refreshed above --> </div>
<div class = "staticpic">
 <img src="picdrawing.png" height="130px" width="99.8%">
 </div> 
 <div class ="refreshpage"> 
<!-- Rest of page content needing refreshed below--> </div>

我的 Web_Run 页面

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<style type="text/css">

</style>

<script>

function showCustomer(str)
{
var xmlhttp;    
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari     php<!-- include('test.php');--> ?>
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","Web_Main.php",true);
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }

xmlhttp.setRequestHeader( "If-Modified-Since", "Sat, 01 Jan 2000 00:00:00 GMT" );
xmlhttp.send(null);
//showCustomer('ALFKI');
setTimeout(function(){showCustomer('load')}, 1500);
}
</script>
</head>
<body onLoad="showCustomer('Load')">

<div id="txtHint"></div>

</body>
</html>

【问题讨论】:

  • 你不能要求浏览器刷新整个页面,除了某个元素。此外,自动刷新页面对我来说听起来像是一种糟糕的用户体验......您可能需要考虑其他解决方案,例如 Ajax 来刷新需要刷新的元素的内容。

标签: javascript jquery html


【解决方案1】:

为什么不尝试使用缓存来缓存静态图像?它仍然会与页面一起“加载”,但它不会再闪烁,因为它会立即从缓存中提供。

确实没有办法刷新除 div 或图像之外的所有内容,但页面上的缓存内容仅出于这个原因而存在。因此,您不必重新加载静态内容。

【讨论】:

  • 以前从未做过任何缓存操作,您将如何为该图片设置图像缓存?
  • 首先你需要一个服务器缓存。您使用什么服务器技术?我假设您正在使用带有 IIS 的 ASP.NET。如果是这样,那就很容易了。
  • 它运行的是带有 Apache 的 Windows 2012 服务器,据我所知,这与其他人运行该服务器一样多。
  • 好吧,我不是 Apache 专家,但我确信为您需要的资源设置缓存很容易。你应该看看这里:stackoverflow.com/questions/447014/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
  • 2014-03-29
  • 2011-09-16
  • 2011-08-28
相关资源
最近更新 更多