假设我有2个div。我必须用数组的前2个元素填充它们。然后自动刷新div,它们应该由阵列的下两个项目自动填充。这应该重复到阵列结束。遍历每个元素后,应自动刷新。
例如,我的数组中有8个元素,2个div(mydiv1和mydiv2)。我希望在每一秒之后,div应该刷新自己。第一次mydiv1包含A1,mydiv2包含A2,第二次mydiv1 = B1,mydiv2 = B2 ....所以4次
我的xyz.php文件
<?php
//echo rand();
$questions=array(
"A1",
"A2",
"B1",
"B2",
"C1",
"C2",
"D1",
"D2"
);
?>
我的index.php文件
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var auto_refresh = setInterval(
function ()
{
$('#mydiv1').load('xyz.php').fadeIn("slow");
}, 1000); // refresh every 1000 milliseconds
});
</script>
</head>
<body>
<div id="mydiv1"> </div>
<div id="mydiv2"> </div>
</body>
答案 0 :(得分:2)
在上一个问题中对Barmar的代码进行了一些更改
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var counter=0;
var auto_refresh = setInterval(function() {
$.getJSON('xyz.php', function(data) {
$("#mydiv1").html(data[counter]);
$("#mydiv2").html(data[counter+1]);
counter+=2;
if (counter > 6) {
clearInterval(auto_refresh);
}
});
}, 1000);
});
</script>
</head>
<body>
<div id="mydiv1"> </div>
<div id="mydiv2"> </div>
</body>
PHP
<?php
$questions=array(
"A1",
"A2",
"B1",
"B2",
"C1",
"C2",
"D1",
"D2"
);
echo json_encode($questions);
?>