[英]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.