用PHP刷新ajaxdiv
我经常尝试刷新页面上的一些元素。我知道这里有一百万个关于这方面的话题,我已经试着让我的工作正常,但这里是我需要刷新的 这是页面加载时生成的代码:用PHP刷新ajaxdiv,ajax,jquery,partial-page-refresh,Ajax,Jquery,Partial Page Refresh,我经常尝试刷新页面上的一些元素。我知道这里有一百万个关于这方面的话题,我已经试着让我的工作正常,但这里是我需要刷新的 这是页面加载时生成的代码: <div id="galleria"> <?php $a = array(); $dir = '../public/wp-content/uploads/2012/01'; if ($handle = opendir($dir)) { while (false !== ($file = r
<div id="galleria">
<?php
$a = array();
$dir = '../public/wp-content/uploads/2012/01';
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (preg_match("/\.png$/", $file)) $a[] = $file;
elseif (preg_match("/\.jpg$/", $file)) $a[] = $file;
elseif (preg_match("/\.jpeg$/", $file)) $a[] = $file;
}
closedir($handle);
}
$totalImgs = count($a);
$imgUsed = array();
for ($j = 0; $j < 100; $j++)
{
do
{
$randIndex = mt_rand(0, $totalImgs);
}
while ($imgUsed[$randIndex] === TRUE);
$imgUsed[$randIndex] = TRUE;
echo "<img src='" . $dir . '/' . $a[$randIndex] . "' />";
}
?>
</div>
我想每10秒自动刷新一次,但不重新加载页面。我读过ajax,这似乎是可能的,但我似乎无法让它工作
所有这些都是显示galleria分区,并在分区内加载100个图像。然后galleria脚本接管并很好地显示它。AJAX和JQuery会更好吗
谢谢你的帮助 在ajax调用中使用
setInterval
函数
<html>
<head>
<script type="text/javascript">
function refresh_gallery(){
$.ajax({
type: "POST",
url: "generate_gallery.php", // your PHP generating ONLY the inner DIV code
data: "showimages=100",
success: function(html){
$("#output").html(html);
}
});
}
$(function() {
refresh_gallery(); //first initialize
setTimeout(refresh_gallery(),10000); // refresh every 10 secs
});
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>
在ajax调用中使用
setInterval
函数
<html>
<head>
<script type="text/javascript">
function refresh_gallery(){
$.ajax({
type: "POST",
url: "generate_gallery.php", // your PHP generating ONLY the inner DIV code
data: "showimages=100",
success: function(html){
$("#output").html(html);
}
});
}
$(function() {
refresh_gallery(); //first initialize
setTimeout(refresh_gallery(),10000); // refresh every 10 secs
});
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>
“AJAX还是jQuery更有效?”——AJAX是一种技术,jQuery是一个库。事实证明,jQuery有一个优秀的AJAX API
让我们将这部分PHP称为“galleria.PHP”。在原始页面加载时,使用良好的ol'
将其插入父PHP页面。现在,最终用户看到了完整的初始化页面
要更新它,您可以使用许多AJAX选项,但最简单的方法是在页面上包含jQuery,然后可以在脚本中使用.load()
:
var updateGallery = setInterval(function() {
$('#someDiv').load('galleria.php');
}, 10000);
有调整的余地。。。可能galleria.php
不包括页面上设置的
。在这种情况下,您将直接装入#galleria
而不是#someDiv
,并为自己节省一个不必要的容器。也许可以通过在不同的作用域中声明$('#someDiv')
对象来缓存它,以便可以重用它。但这是总的要点。“AJAX比jQuery更好吗?”——AJAX是一种技术,jQuery是一个库。事实证明,jQuery有一个优秀的AJAX API
让我们将这部分PHP称为“galleria.PHP”。在原始页面加载时,使用良好的ol'
将其插入父PHP页面。现在,最终用户看到了完整的初始化页面
要更新它,您可以使用许多AJAX选项,但最简单的方法是在页面上包含jQuery,然后可以在脚本中使用.load()
:
var updateGallery = setInterval(function() {
$('#someDiv').load('galleria.php');
}, 10000);
有调整的余地。。。可能galleria.php
不包括页面上设置的
。在这种情况下,您将直接装入#galleria
而不是#someDiv
,并为自己节省一个不必要的容器。也许可以通过在不同的作用域中声明$('#someDiv')
对象来缓存它,以便可以重用它。但这是一般要点。正如我所写的,您可以用jQuery ajax调用填充div
<html>
<head>
<script type="text/javascript">
function refresh_gallery(){
$.ajax({
type: "POST",
url: "generate_gallery.php", // your PHP generating ONLY the inner DIV code
data: "showimages=100",
success: function(html){
$("#output").html(html);
}
});
}
$(function() {
refresh_gallery(); //first initialize
setTimeout(refresh_gallery(),10000); // refresh every 10 secs
});
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>
函数刷新\u库(){
$.ajax({
类型:“POST”,
url:“generate_gallery.php”//您的php只生成内部DIV代码
数据:“showimages=100”,
成功:函数(html){
$(“#输出”).html(html);
}
});
}
$(函数(){
刷新_gallery();//首先初始化
setTimeout(refresh_gallery(),10000);//每10秒刷新一次
});
正如我所写,您可以用jQuery ajax调用填充div
<html>
<head>
<script type="text/javascript">
function refresh_gallery(){
$.ajax({
type: "POST",
url: "generate_gallery.php", // your PHP generating ONLY the inner DIV code
data: "showimages=100",
success: function(html){
$("#output").html(html);
}
});
}
$(function() {
refresh_gallery(); //first initialize
setTimeout(refresh_gallery(),10000); // refresh every 10 secs
});
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>
函数刷新\u库(){
$.ajax({
类型:“POST”,
url:“generate_gallery.php”//您的php只生成内部DIV代码
数据:“showimages=100”,
成功:函数(html){
$(“#输出”).html(html);
}
});
}
$(函数(){
刷新_gallery();//首先初始化
setTimeout(refresh_gallery(),10000);//每10秒刷新一次
});
因此,一个指向外部资源的链接算是一个更好的答案,因为他能够在不到一分钟的时间内把它发出吱吱声(因为我写了一些有实质内容的东西)?答案应该是独立的。甚至连投票都没有?有时我想放弃堆栈溢出-DSo一个指向外部资源的链接算是一个更好的答案,因为他能够在不到一分钟的时间内把它弄清楚(因为我写了一些有实质内容的东西)?答案应该是独立的。甚至连投票都没有?有时我想放弃堆栈溢出-D