我正在尝试在我的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 Startedor
6/11/2016 9:31:12 AM Script StoppedThe 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>';更多推荐
发布评论