簡體   English   中英

關於自動刷新的一部分<div>在php文件中

[英]About auto refresh a part of <div> in the php file

我想在 php 文件中測試自動刷新。 下面是我的代碼。 但是,我做不到。 我不知道是什么問題?

<html>
<head>
<title>TEST</title>
</head>
<body>
<script type="text/javascript">
    window.onload = setupRefresh;
    function setupRefresh()
    {
        setInterval("refreshBlock();",1000);
    }

    function refreshBlock()
    {
       $('#test').load("test.php");
    }
  </script>

<div id="test">
    <?php echo rand(); ?>
</div>

</body>
</html>

這樣做。

<html>
<head>
<title>TEST</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
    window.onload = setupRefresh;
    function setupRefresh()
    {
        setInterval(refreshBlock,1000); //Call function like this
    }

    function refreshBlock()
    {
       $('#test').load("test.php");
    }
  </script>

<div id="test">
    <?php echo rand(); ?>
</div>

</body>
</html>

您使用 jQuery 代碼,因此請添加 jQuery 庫您可以從這里獲取鏈接

https://www.w3schools.com/jquery/jquery_get_started.asp

這將正常工作

<html>
<head>
  <title>TEST</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
window.onload = setupRefresh;
function setupRefresh()
{
    setInterval(refreshBlock,1000); 
}

function refreshBlock()
{
    $('#test').load("http://localhost/test.php"); // just update your test file url
}
 </script>

 <div id="test">
  <?php echo rand(); ?>
 </div>

 </body>
</html>

這是你的 test.php 文件(示例代碼)

<?php
 echo date("Y-m-d H:i:s");

?>

輸出將是這樣的,並且自動更新時間

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM