Dut*_*432 37
您需要一个getTable.php
显示您的表格的页面,而不是其他内容:没有页眉,页脚等.
PHP(getTable.php) - 这可以是任何服务器端代码(asp,html等).
<?php
echo '<table><tr><td>TEST</td></tr></table>';
?>
Run Code Online (Sandbox Code Playgroud)
然后,在您的JS中,您可以使用以下load()
方法轻松刷新表:
HTML
<div id="tableHolder"></div>
Run Code Online (Sandbox Code Playgroud)
JS
<script type="text/javascript">
$(document).ready(function(){
refreshTable();
});
function refreshTable(){
$('#tableHolder').load('getTable.php', function(){
setTimeout(refreshTable, 5000);
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
使用ajax,以下示例在jQuery中:
$(function() {
var prevAjaxReturned = true;
var xhr = null;
setInterval(function() {
if( prevAjaxReturned ) {
prevAjaxReturned = false;
} else if( xhr ) {
xhr.abort( );
}
xhr = $.ajax({
type: "GET",
data: "v1="+v1+"&v2="+v2,
url: "location/of/server/script.php",
success: function(html) {
// html is a string of all output of the server script.
$("#element").html(html);
prevAjaxReturned = true;
}
});
}, 5000);
});
Run Code Online (Sandbox Code Playgroud)
success函数假定您的服务器脚本在id为'element'的元素中输出要替换的html.
您应该有一个页面返回信息并使用Ajax/jQuery提取数据.
<div class="result"></div>
setInterval(function() {
$.get('table.php', function(data) {
$('#result').html(data);
});
}, 5000);
Run Code Online (Sandbox Code Playgroud)
小智 2
这是另一个供您使用的选项。该解决方案使用IIFE,它优于 setInterval。您可以通过上面的链接阅读有关 IIFE 的更多信息。
JavaScript:
var $results = $('#results'),
loadInterval = 5000;
(function loader() {
$.get('script.php', function(html){
$results.hide(200, function() {
$results.empty();
$results.html(html);
$results.show(200, function() {
setTimeout(loader, loadInterval);
});
});
});
})();
Run Code Online (Sandbox Code Playgroud)
HTML:
<div id="results"></div>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
70818 次 |
最近记录: |