我在 HTML
文件中有一个脚本,它每 30 秒运行一次 php
文件。 php
文件旨在更改 div 的 innerHTML。我似乎没有找到替换它的方法。
我正在使用 load()
但有几个 div
我想每 30 秒更新一次,我不想让服务器获取那么多要求。所以我正在寻找一个只有 php
获取然后更改那几个 div 中的 innerhtml
这是我得到的:
HTML
:
<script type="text/javascript">
$(document).ready(function () {
$.get('php.php');
});
setInterval(function(){
$.get('php.php');
}, 30000);
</script>
<div id="foo"></div>
php.php
:
document.getElementById("foo").innerHTML = '<?php
// some php code
?>';
我认为问题在于我无法从父 HTML
文件中获取元素。我不知道如何解决这个...
我们将不胜感激任何建议!
最佳答案
您正在 reshape jQuery 的 load()
function fetchContent() {
$("#foo").load('php.php');
window.setTimeout(fetchContent, 30000);
}
$(fetchContent);
服务器应该只返回您要显示的 HTML 内容。
编辑
由于您的评论与原始问题不同。
function fetchContent() {
$("#foo").get('php.php', function(data) {
var html = $("<div/>").html(data);
$("#foo").html( html.find("#Section1").html() );
$("#bar").html( html.find("#Section2").html() );
$("#asd").html( html.find("#Section3").html() );
window.setTimeout(fetchContent, 30000);
});
}
$(fetchContent);
您可能想要添加一个 onError 处理程序,并且可能想要为 Ajax 调用设置无缓存选项。
关于php - 从外部 php 文件更改 innerHTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16534745/