javascript - 从文本文件填充 HTML Ticker
问题描述
我正在尝试为非精通技术的人设置一种简单的方法来更新将出现在将发布到我们的 BrightSign 显示系统的网站上的选取框代码。我认为他们可以轻松地将他们想要的内容输入文本文件,保存,然后将网站“重新发布”到我们的显示系统。所有文件(index.html、style.css、tickerText.txt)都将保存在本地计算机上,供负责人编辑选取框。
当我在选取框标签之间输入文本时,一切正常,但是当我尝试从文本文件中提取信息时,它会滚动显示大量“空白”而不是长文本。下面的脚本显示它何时正确显示(显示在图片链接中 - 不允许我嵌入)。
<!-- Bottom Marquee Ticker -->
<div class="3-row-padding w3-center">
<div class="w3-container ocacity75"">
<div class="w3-card-4 w3-white w3-round ticker">
<marquee>This is a test and so is this</marquee>
</div>
</div>
</div>
下面的脚本是当我尝试从文本文件中填充选框然后弄乱我的格式(如图所示 - 不允许我嵌入)。
<!-- Bottom Marquee Ticker -->
<div class="3-row-padding w3-center">
<div class="w3-container ocacity75"">
<div class="w3-card-4 w3-white w3-round ticker">
<marquee><object type="text/html" data="./TickerText.txt"></object></marquee>
</div>
</div>
</div>
我什至尝试向对象标签添加内联样式以尝试使选取框不那么大并增加文本大小,但似乎没有任何效果。尝试读取文本文件时,我可能会遇到这个错误。我尝试了一些 JavaScript,但我无法让它工作,虽然只是一个选取框标签就可以了。任何帮助将不胜感激。
为了确认,我没有使用 AJAX,因为网站不会在显示系统上显示其更改,直到我们再次点击发布按钮。因此,在不刷新的情况下对网站进行实时更新将是矫枉过正。
解决方案
好吧,你可以试试这个:
var input = document.getElementById("myFile");
var output = document.getElementById("output");
input.addEventListener("change", function() {
if (this.files && this.files[0]) {
var myFile = this.files[0];
var reader = new FileReader();
reader.addEventListener('load', function(e) {
output.textContent = e.target.result;
});
reader.readAsBinaryString(myFile);
}
});
#styleTest {
color: green;
}
<input type="file" id="myFile">
<div id="styleTest">
<div id="output"></div>
</div>
这将满足您的需求,如果需要,文本文件中的数据也可以进行格式化。
正如A Haworth在评论中所说,
该<marquee></marquee>
标签现已弃用。
此外,您甚至可以通过执行以下操作绕过文本文件:
(注意:目前这个更适合您的需求。)
var input = document.getElementById("input");
var output = document.getElementById("output");
var txtInSub = document.getElementById("txtInSub");
function addData() {
output.innerHTML = input.value;
}
window.addEventListener("keydown", checkKeyPressed, false);
function checkKeyPressed(e) {
e = e || window.event;
var key = e.which || e.keyCode;
// key == "13" is the enter key
if (key == "13") {
txtInSub.click();
console.log({keyPressed: "Enter", functionExecution: "Submitted the Text Input Field", contentPosted: input.value});
}
}
#styleTest {
color: green;
}
#inSubFld {
text-align: center;
width: 98.22%;
position: absolute;
left: 0;
top: 60px;
}
#input {
width: 100%;
padding: 10px;
display: block;
}
#txtInSub {
display: none;
}
<div id="styleTest">
<div id="output"></div>
</div>
<div id="inSubFld">
<input id="input" type="text" autofocus> <!-- with autofocus it should force it so that you could type in the input field without needing to click it -->
<button id="txtInSub" onclick="addData()">Submit</button>
<p>Press Enter to Submit the content in the field above.</p>
</div>
推荐阅读
- flutter - Flutter TextFormField 换行并自动适配高度
- mysql - 更新与其他列具有一个或多个相同值的列值
- spring-boot - 海王星DB图遍历管理的建议
- c# - 急切加载的数据错误,说明上下文已处理
- c++ - 比较2个大小不等的地图c ++
- excel - sumproduct 包含 excel 中的其他函数
- php - 是否可以从“EUR”或“GBP”等货币代码动态转换货币符号?
- git - Intelij IDEA ssh-key git 存储库连接被拒绝
- python - 在 Keras LSTM 中添加双向会导致 RuntimeError('您必须在使用之前编译模型。')
- c++ - boost xml parsingl 将 xml 的路径作为变量发送