[英]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.