我有一个PHP文件,其中包含一个SVG部件。在SVG部分中,我想显示一个PHP变量,它每3秒更新一次。我尝试过使用jquery,但这只是清除了空间。这是我使用的jquery代码的代码。SVG部分必须刷新:
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
setInterval(function updateDiv(){
$( "#LA" ).load(window.location.href + "#LA")
}, 3000);
})
</script>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<text id="LA" fill="#a3a3a3" font-size="35" font-family="FranklinGothic-Heavy, Franklin Gothic" font-weight="800">
<tspan x="7.902" y="32">
<?php echo $A_array[0][0]?>
</tspan>
</text>
</svg
我还试图通过搜索“刷新PHP数据”来寻找其他解决方案,但答案大多是基于更新MySQL数据的。有人能帮我吗?
发布于 2021-02-06 09:55:07
代码中有两个错误:
重新加载时,-you在#LA之前缺少一个空格
-you必须定义一个更深层次的id="LA“:在tspan内部。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Auto reload SVG</title>
<meta name="description" content="Auto reload SVG">
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script>
$(document).ready(function(){
setInterval(function updateDiv(){
$( "#test" ).load(window.location.href + " #test")
$( "#LA" ).load(window.location.href + " #LA")
}, 2000);
})
</script>
</head>
<body>
<h1>
<span id="test">
<?php echo rand(10,100); ?>
</span>
</h1>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<text fill="#a3a3a3" font-size="35" font-family="FranklinGothic-Heavy, Franklin Gothic" font-weight="800">
<tspan x="7.902" y="32" id="LA">
<?php echo rand(100,1000); ?>
</tspan>
</text>
</svg>
</body>
</html>
上述解决方案的一个问题是,由于要显示更高的频率和更多的数据号,它不能很好地扩展,因为load()方法加载了包含所有脚本的完整页面。然后过滤出所提供的#id内容。
最好是将数据从页面本身中分离出来,并使用jQuery get()方法调用这个数据端点。
您可以看到它从84 it降到了0.26KB (不包括每次调用的FaviconLoader.jsm )。
https://stackoverflow.com/questions/65924422
复制相似问题