.load 使用 file.php 重新加载 div

发布于 2024-11-17 06:07:11 字数 410 浏览 0 评论 0原文

用 php 文件重新加载 div 的方法是什么?我在 HTML 页面中定义了一个这样的 div。

HTML

<div id="tnav"
   <?php require("loadtree.php"); ?>
</div>

我有一个 PHP 文件,它执行数据库操作,然后调用 div 上的 load 方法来重新加载。

<?php
 . 
 .
 .
 echo "<script type='text/javascript'> $('#tnav').load('loadtree.php'); </script>";
?>

该脚本在源中回显,但这不会导致页面重新加载。可能是什么问题?

What's the way to reload a div with a php file. I have a div defined like this in the HTML page.

HTML

<div id="tnav"
   <?php require("loadtree.php"); ?>
</div>

I have a PHP file that peforms database actions then calls the load method on the div to reload.

<?php
 . 
 .
 .
 echo "<script type='text/javascript'> $('#tnav').load('loadtree.php'); </script>";
?>

The script echoes in the source, but this does not cause the page to reload. What could be the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

拥醉 2024-11-24 06:07:11

我正在做类似的事情,我需要在一个页面上有多个 div,并将 GET 变量传递给包含的 div,然后当触发触发器时,我需要使用 jQuery 分别重新加载每个单独的 div(在本例中,在图片上传)。如果这对任何人有任何帮助,我就是这样做的:

在主脚本中,我声明了多个 div,每个 div 都有一个由构造循环生成的唯一 id:

for($i=1;$i<$filecount+1;$i++)
{
?>            
    <div class="someclass" style="width:100%">
        <div id="imgdiv<?php echo $i; ?>">
        </div> 
    </div>
    <?php
    //assign a colorbox link to each to trigger colorbox to open that specific instance (and thus fire that specific trigger when closed) - class="iframe[#]" is used by colorbox to fire. ?> 
    <a class="iframe<?php echo $i; ?>" href="add-new-img.php?id=<?php echo $id; ?>&imgno=<?php echo $i; ?>">Edit Image <?php echo $i; ?></a>
} 

接下来,我使用另一个循环来初始化 JS onload 事件(现在这个我觉得可以比使用 10 个单个 onload 事件更有效..):

<script>                
window.onload = function(){
   <?php
   for($i=1;$i<11;$i++)
   {
    ?>
    $("#imgdiv<?php echo $i; ?>").load('php/show-img.php?id=<?php echo $i; ?>&name=<?php echo $name; ?>');      
    <?php
   }
   ?>
}
</script>

最后,我希望每个 div 在相关触发器被触发时独立于其他 div 重新加载。在本例中,我的触发器是一个正在关闭的 colorbox,因此我为我需要的每个触发器分配了一个 colorbox 实例,并将 onClosed 触发器绑定到每个 div 的函数。由于每个 div 都分配了一个唯一的名称,因此我可以按如下方式执行此操作:

<script>
  $(document).ready(function(){
  //Assign an individual colorbox instance to each div
  <?php for($i=1;$i<$filecount+1;$i++)
    { ?>
        $(".iframe<?php echo $i; ?>").colorbox({iframe:true, width:"90%", height:"90%", opacity:.8, fixed:true, overlayClose:false, onClosed:function(){$('#imgdiv<?php echo $i; ?>').fadeOut('slow', function(){       
        $("#imgdiv<?php echo $i; ?>").load('php/show-img.php?id=<?php echo $i; ?>&name=<?php echo $name; ?>');          
        $('#imgdiv<?php echo $i; ?>').fadeIn('slow');});
        }});
        <?php
    } ?>        
  });
</script>

无论是否包含在颜色盒触发线中,该功能都是相同的。

我意识到这是一篇巨大的帖子,超出了您实际要求的内容,但我花了几周的时间寻找这样的解决方案,但无法找到解决方案,所以希望这会对某人有所帮助!

I was doing something similar and I needed to have multiple divs on one page with GET variables passed to the included divs, then I needed to reload each individual div separately with jQuery when a trigger was fired (closing a colorbox in this instance, after an image upload). If it is of any help to anyone, this is how I did it:

In the main script I declared multiple divs, each with a unique id generated by the construction loop:

for($i=1;$i<$filecount+1;$i++)
{
?>            
    <div class="someclass" style="width:100%">
        <div id="imgdiv<?php echo $i; ?>">
        </div> 
    </div>
    <?php
    //assign a colorbox link to each to trigger colorbox to open that specific instance (and thus fire that specific trigger when closed) - class="iframe[#]" is used by colorbox to fire. ?> 
    <a class="iframe<?php echo $i; ?>" href="add-new-img.php?id=<?php echo $id; ?>&imgno=<?php echo $i; ?>">Edit Image <?php echo $i; ?></a>
} 

Next I used another loop to initialise the JS onload events (now this bit I feel could be done more efficiently than using 10 single onload events..):

<script>                
window.onload = function(){
   <?php
   for($i=1;$i<11;$i++)
   {
    ?>
    $("#imgdiv<?php echo $i; ?>").load('php/show-img.php?id=<?php echo $i; ?>&name=<?php echo $name; ?>');      
    <?php
   }
   ?>
}
</script>

Finally I wanted each div to reload independently of the others when the relevant trigger was fired. In this case my trigger was a colorbox being closed, so I assigned an instance of colorbox for each of the triggers I needed and tied the onClosed trigger to a function for each div. As each div had a unique name assigned to it I could do this as follows:

<script>
  $(document).ready(function(){
  //Assign an individual colorbox instance to each div
  <?php for($i=1;$i<$filecount+1;$i++)
    { ?>
        $(".iframe<?php echo $i; ?>").colorbox({iframe:true, width:"90%", height:"90%", opacity:.8, fixed:true, overlayClose:false, onClosed:function(){$('#imgdiv<?php echo $i; ?>').fadeOut('slow', function(){       
        $("#imgdiv<?php echo $i; ?>").load('php/show-img.php?id=<?php echo $i; ?>&name=<?php echo $name; ?>');          
        $('#imgdiv<?php echo $i; ?>').fadeIn('slow');});
        }});
        <?php
    } ?>        
  });
</script>

The function is the same whether encased in the colorbox firing line or not.

I realise this is a huge post and more than you've actually asked for, but I spent weeks looking for a solution like this and couldn't fine one, so hopefully this will help someone!

云醉月微眠 2024-11-24 06:07:11

PHP 在页面加载之前在服务器端执行。页面加载后,PHP 无法访问该页面。您应该考虑使用 JavaScript(或 jQuery?)来重新加载页面,具体取决于您具体想要做什么。

在脚本输出中添加倒计时器以使用 JavaScript 重新加载页面。

PHP is executed server-side, before the page loads. After the page loads, PHP has no access to the page. You should look into using either JavaScript (or jQuery?) to reload the page, depending on what you are specifically trying to do.

Add a countdown timer in the script output to reload the page using JavaScript.

迷离° 2024-11-24 06:07:11

如果您添加应在页面加载时执行的内容,请

$(function(){ 
    /* your content */ 
});

按照您的情况这样做,

echo "<script type='text/javascript'> 
                        $(function(){
                                $('#tnav').load('loadtree.php');
                        }); 
      </script>";

If you add your contents which should be executed on page load, do it this way

$(function(){ 
    /* your content */ 
});

so in your case,

echo "<script type='text/javascript'> 
                        $(function(){
                                $('#tnav').load('loadtree.php');
                        }); 
      </script>";
花开雨落又逢春i 2024-11-24 06:07:11

试试这个

echo "<script type='text/javascript>
    $(document).ready(function() {
        $('#tnav').load('loadtree.php');
    });
</script>";

或者,如果您想让代码更具可读性,请在 php 标记之外编写脚本,以便突出显示语法。

?>
<script type="text/javascript">
    $(document).ready(function() {
        $('#tnav').load('loadtree.php');
    });
</script>
<?php

Try this

echo "<script type='text/javascript>
    $(document).ready(function() {
        $('#tnav').load('loadtree.php');
    });
</script>";

Or, if you want to make your code a little more readable, write the script outside of the php tags so it gets syntax highlighted.

?>
<script type="text/javascript">
    $(document).ready(function() {
        $('#tnav').load('loadtree.php');
    });
</script>
<?php
逆光飞翔i 2024-11-24 06:07:11

必须将 div 更改为 iframe,然后像这样重新加载 iframe。

         $('#tnav').attr('src','loadtreeb.php');

Had to change the div to an iframe then reloaded the iframe like this.

         $('#tnav').attr('src','loadtreeb.php');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文