簡體   English   中英

更新 HTML Object 數據

[英]Update HTML Object Data

我對 HTML 相當陌生,我正在嘗試制作一個不斷讀取文本文件的 HTML 頁面,這樣當文本文件的內容發生更改時,HTML 頁面也會更改以反映它。 我當前的代碼如下;

<!DOCTYPE html>
<html>
<title>File Contents</title>
<h1>File contents of input.txt<br>Below are the contents of the file input.txt, in the same directory as this file</h1>
<div><object data="Input.txt"></object></div>
</html>

但是,此腳本僅加載文本文件一次,並且不會顯示任何后續修改。

有沒有辦法只用 HTML 和 Javascript 來做到這一點?

此腳本將每 10 秒(10000 毫秒)重新加載<object>

<!DOCTYPE html>
<html>
<title>File Contents</title>
<h1>File contents of input.txt<br>Below are the contents of the file input.txt, in the same directory as this file</h1>
<div><object data="Input.txt"></object></div>
<script type="text/javascript">
setInterval(() => {
    let obj = document.getElementsByTagName('object')[0];
    obj.setAttribute('data', obj.getAttribute('data'));
}, 10000);
</script>
</html>

暫無
暫無

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

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