![](/img/trans.png)
[英]How can I move a variable over to another html file and change the text on the html file based on the variable
[英]How do I read the data from text file into a variable so I can change the color based on the value?
我項目的目標是將不斷變化的數據流從文本文件讀取為html文件,然后更改字體顏色。
我嘗試了多種方法來讀取文本文件,例如使用iframe並嘗試使用fetch命令,但無法成功使用這些方法來完成我的目標。
<body>
<script type="text/javascript">
function print() {
fetch("test.txt")
.then(function(response) { return response.text; })
.then(function(text) {
var num = parseInt(text);
if(num>250){
num=fontcolor("green")
}
else()
{
num=fontcolor("red")
}
});
}
setInterval(print, 1000); //run function print() every 1 second
</script>
<META HTTP-EQUIV="refresh" CONTENT="1">
</body>
結果應該是一個html文件,當在瀏覽器中打開該文件時,它將在文本文件中顯示數字,並根據給定的數字更改顏色。
出於安全原因,您無法在本地計算機中獲取文本文件。
您可以參考此鏈接以獲取有關在Chrome中禁用同一來源策略的更新安全性
您可以將文件托管為免費托管。 我更正了您的.then(response => response.text())
更正
<body>
<script type="text/javascript">
function print() {
fetch("test.txt")
.then(response => response.text())
.then(text => {
var num = parseInt(text);
if(num>250){
console.log(num);
document.body.style.backgroundColor = "green";
}
else
{
//num=fontcolor("red")
console.log(num);
document.body.style.backgroundColor = "blue";
}
})
}
setInterval(print, 1000); //run function print() every 1 second
</script>
<META HTTP-EQUIV="refresh" CONTENT="1">
</body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.