Javascript读取.txt文件并以HTML格式显示(Javascript reading .txt file and displaying in HTML)

编程入门 行业动态 更新时间:2024-10-22 02:53:30
Javascript读取.txt文件并以HTML格式显示(Javascript reading .txt file and displaying in HTML)

我正在尝试在我的HTML页面上从同一目录中的.txt文件(status.txt)读取JavaScript,并根据.txt文件中的信息以两种不同的字体颜色显示内容。 我目前在页面上显示文本就好了,但我想让它更加引人注目。 这是我当前的代码,用于显示基本#ccc hex的文本。

<script type="text/javascript"> loadXMLDoc(); </script> <script type="text/javascript"> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("status").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET", "status.txt", true); xmlhttp.send(); } </script> <h3 id="status" style="padding-right: 7px; padding-left: 7px; margin-top: 2px; font-size: 11px; color: #cccccc"> Status </h3>

每次运行.vbs文件时都会覆盖文本文件内容。 它会说:

6/11/2016 8:58:30 AM Script Started

要么

6/11/2016 9:31:12 AM Script Stopped

唯一改变的是时间戳。 我希望文本在显示“(timestamp)Script Stopped”时显示为红色,当它显示“timestamp”Script Started时显示为绿色。 如果有人可以提供帮助,那就太好了!

I'm trying to have JavaScript on my HTML page read from a .txt file (status.txt) in the same directory and display the contents in two different font colors based on the information in the .txt file. I currently have it displaying the text on the page just fine, but I wanted to make it a bit more noticeable. Here is my current code to display the text with the basic #ccc hex.

<script type="text/javascript"> loadXMLDoc(); </script> <script type="text/javascript"> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("status").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET", "status.txt", true); xmlhttp.send(); } </script> <h3 id="status" style="padding-right: 7px; padding-left: 7px; margin-top: 2px; font-size: 11px; color: #cccccc"> Status </h3>

The text file contents are overwritten every time a .vbs file is ran. It will either say:

6/11/2016 8:58:30 AM Script Started

or

6/11/2016 9:31:12 AM Script Stopped

The only thing that changes is the timestamp. I would like the text to display as red when it says "(timestamp) Script Stopped" and green when it says "timestamp) Script Started". If anyone could help, that'd be great!

最满意答案

var color = xmlhttp.responseText.indexOf('Started') !== -1 ? 'green' : 'red'; document.getElementById("status").innerHTML = '<span style="color: ' + color + '">' + xmlhttp.responseText + '</span>'; var color = xmlhttp.responseText.indexOf('Started') !== -1 ? 'green' : 'red'; document.getElementById("status").innerHTML = '<span style="color: ' + color + '">' + xmlhttp.responseText + '</span>';

更多推荐

本文发布于:2023-08-07 06:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463112.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并以   格式   文件   Javascript   txt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!