对同一张图片可以采用随机参数的方式,每隔5秒请求一次最新的该图片 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title><script> window.onload=function() { var oBox=document.getElementById('box'); var oImg=oBox.getElementsByTagName('img')[0]; setInterval(function(){ oImg.src=oImg.src.replace(/\?.*?$/,'')+'?'+new Date().getTime(); },5000); } </script> </head><body> <div class="box" id="box"> <img src="http://img0.bdstatic.com/img/image/shouye/sjsdzgtg271134.jpg" /> </div> </body> </html>
不好意思 我改了你的 <script> window.onload=function() { var oBox=document.getElementById('mydiv'); var oImg=oBox.getElementsByTagName('img')[0]; setInterval(function(){ oImg.src=oImg.src.replace(/\?.*?$/,'')+'?'+new Date().getTime(); },5000); } </script>可是他沒有反應?? 不太理解
我改的是我的DIV 但是我的照片完全沒有改變
你的图片根本就没有改变,所以不管怎么重新加载都是同一张图片,明白没?实在不行你再试试这个.. window.onload=function() { var oBox=document.getElementById('mydiv'); var oImg=oBox.getElementsByTagName('img')[0]; setInterval(function(){ var r = Math.ceil(Math.random()*5); oImg.src='img_'+r+'.jpg'; },5000); }
直接把代码拷给你,你来研究.<script type="text/javascript"> function tupian_lundong_1() { var a = document.getElementById("tupian_lundong"); a.src = "image/lundong_1.jpg"; var b = document.getElementById("tupian_gundong_a"); b.href = "<?php echo $gd_http_1;?>";
setTimeout('tupian_lundong_2()',5000); }function tupian_lundong_2() { var a = document.getElementById("tupian_lundong"); a.src = "image/lundong_2.jpg"; var b = document.getElementById("tupian_gundong_a"); b.href = "<?php echo $gd_http_2;?>";
setTimeout('tupian_lundong_3()',5000); }function tupian_lundong_3() { var a = document.getElementById("tupian_lundong"); a.src = "image/lundong_3.jpg"; var b = document.getElementById("tupian_gundong_a"); b.href = "<?php echo $gd_http_3;?>";
但是其他的DIV不會閃爍
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title><script>
window.onload=function()
{
var oBox=document.getElementById('box');
var oImg=oBox.getElementsByTagName('img')[0];
setInterval(function(){
oImg.src=oImg.src.replace(/\?.*?$/,'')+'?'+new Date().getTime();
},5000);
}
</script>
</head><body>
<div class="box" id="box">
<img src="http://img0.bdstatic.com/img/image/shouye/sjsdzgtg271134.jpg" />
</div>
</body>
</html>
<script>
window.onload=function()
{
var oBox=document.getElementById('mydiv');
var oImg=oBox.getElementsByTagName('img')[0];
setInterval(function(){
oImg.src=oImg.src.replace(/\?.*?$/,'')+'?'+new Date().getTime();
},5000);
}
</script>可是他沒有反應??
不太理解
window.onload=function()
{
var oBox=document.getElementById('mydiv');
var oImg=oBox.getElementsByTagName('img')[0];
setInterval(function(){
var r = Math.ceil(Math.random()*5);
oImg.src='img_'+r+'.jpg';
},5000);
}
因為我有夾雜LUA 在裡面
我需要讓他可以完全更新
如果整個頁面的話可以跑
但是這個DIV更新的方法 並不能讓他自己全部重新跑一次
所以造成他圖片並沒有改變
用定时器,通过AJAX动态去获取数据。
請問該怎麼用AJAX
這個我不是很熟悉
3Q
是指這個嗎??
<%
if port3_status=="Up" then
%>
<img src="<%=resource%>/cbi/on.gif" border="0" style="position:relative;left:25px;top:-99px;" title="Port3 on"/>
<%
else
%>
<img src="<%=resource%>/cbi/off.gif" border="0" style="position:relative;left:25px;top:-99px;" title="Port3 off"/>
<%
end
%>
</div>這是我的DIV
所以要在后面加上'?'+new Date().getTime();,这样每次的要求都不一样,(哪怕是同一张图片)也会刷新。如果你是每隔5秒要到某个地方取下次显示的文件名,不知道你用过jQuery没有,直接查一下get方法,
get("能返回文件名的URL")就行了呗。
<id="i_01" img />JS
window.onload = function() {
var _document, window, i = -1, il = ['1.jpg', '2.jpg', '3.jpg', '4.jpg'];
setInterval(function() {
_document.getElementById('i_01').src = il[i === il.length - 1 ? i = 0 : ++i];
}, 5000);
};不知道能满足你的要求吗
你是要每5秒钟检测一次,判断是显示图片on.gif或者是off.gif?
所以每次重新整理時他會去判斷port3_status等於多少去選擇圖片
但是當我port3_status不一樣時
oImg.src 並沒有幫我把圖片換掉
重新整理之後
他卻可以把DIV的圖換掉
你是要每5秒钟检测一次,判断是显示图片on.gif或者是off.gif?我是依照這個port3_status 來選擇圖片的沒錯
你是要每5秒钟检测一次,判断是显示图片on.gif或者是off.gif?我是依照這個port3_status 來選擇圖片的沒錯这个应该是5秒钟给服务器发一个请求,然后服务器返回一个结果,再根据服务器返回的结果判断显示哪张图片..
但是我現在想要每五秒更新這個DIV
但是其他的不刷新
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<div id="divA"><img src="/bm_icon.jpg" /></div>
<script type="text/javascript">
setInterval(function(){
var oDiv = document.getElementById("divA");
var oImg = oDiv.getElementsByTagName("img")[0];
oImg.src=oImg.src.replace(/\?.*/,'')+"?"+Math.random();
},5000)
</script>
</body>
</html>
請問大大這樣的程式碼可以解釋一下嗎 好像沒有重新更新DIV?
还有一些建议:既然你的需求只要两张图片,那就根本没有必要改变图片地址一直向服务器发送请求,直接放上两张图片然后根据返回值判断显示哪张就行了。
还有,你最好先去了解一下ajax。
还有一些建议:既然你的需求只要两张图片,那就根本没有必要改变图片地址一直向服务器发送请求,直接放上两张图片然后根据返回值判断显示哪张就行了。
还有,你最好先去了解一下ajax。
好 謝謝 我會朝這個方向試試
function tupian_lundong_1()
{
var a = document.getElementById("tupian_lundong");
a.src = "image/lundong_1.jpg";
var b = document.getElementById("tupian_gundong_a");
b.href = "<?php echo $gd_http_1;?>";
setTimeout('tupian_lundong_2()',5000);
}function tupian_lundong_2()
{
var a = document.getElementById("tupian_lundong");
a.src = "image/lundong_2.jpg";
var b = document.getElementById("tupian_gundong_a");
b.href = "<?php echo $gd_http_2;?>";
setTimeout('tupian_lundong_3()',5000);
}function tupian_lundong_3()
{
var a = document.getElementById("tupian_lundong");
a.src = "image/lundong_3.jpg";
var b = document.getElementById("tupian_gundong_a");
b.href = "<?php echo $gd_http_3;?>";
setTimeout('tupian_lundong_1()',5000);
}setTimeout('tupian_lundong_1()',2000);
//以上代码图片切换
</script>
謝謝 可是我需要用我的
<%
if port3_status=="Up" then
%>
<img src="<%=resource%>/cbi/on.gif" border="0" style="position:relative;left:25px;top:-99px;" title="Port3 on"/>
<%
else
%>
<img src="<%=resource%>/cbi/off.gif" border="0" style="position:relative;left:25px;top:-99px;" title="Port3 off"/>
<%
end
%>
這段去判斷要哪張圖片
所以不能只是換圖片
setInterval(
$.ajax(
url:*,
success:function(data){
//填入加载htm
}
),5000)
$.ajax(
url:*,
success:function(data){
192.168.1.100;
}
),5000)謝謝高手~但是好像沒反應???