简体   繁体   English

HTML JavaScript 从文件打印到页面上

[英]HTML JavaScript print from file onto page

I have a problem with a little site (it's intended to work as a local site) I try to create.我尝试创建的一个小站点(它旨在用作本地站点)有问题。 I want it to print text from local txt file onto the page.我希望它将本地 txt 文件中的文本打印到页面上。 I want it to display it like this one,我想让它像这样显示,

<script type="text/javascript">
var arr = ['Heading 1','Para1','Heading 2','Para2','Heading 3','Para3'];
var result = arr.map((val, i) => i%2===0 ? `<h2>${val}</h2>` : `<p>${val}</p>`).join('');
document.getElementById('test').innerHTML = result ;
</script>

but I want it to do it from a file, like this one但我希望它从一个文件中完成,就像这个

<script>
 var fileInput = document.getElementById('inputfile');
 fileInput.addEventListener('change', function(e) {
     var file = fileInput.files[0];
     var reader = new FileReader();
     reader.onload = function(e) {
         document.getElementById('output').innerText = reader.result;
 };
     reader.readAsText(file);   
 });
 </script>

I tried to merge them together like this我试图像这样将它们合并在一起

   <script>
    var fileInput = document.getElementById('inputfile');
    fileInput.addEventListener('change', function(e) {
        var file = fileInput.files[0];
        var reader = new FileReader();
        reader.onload = function(e) {
            document.getElementById('output').innerHTML = reader.result.split("\n").map((val, i) => i%2===0 ? <h2>${val}</h2> : <p>${val}</p>).join('');
        };
    reader.readAsText(file[0]);   
 });
 </script>

But something is still not working right (after choosing the file to read, it does nothing) and I am not sure what am I doing wrong.但是有些东西仍然无法正常工作(在选择要读取的文件后,它什么也没做)而且我不确定我做错了什么。 I am green in javascript, so I would appreciate any help in that matter.我在 javascript 方面是绿色的,所以我很感激在这方面的任何帮助。

Actually, now that I read that again - the only issue with your example is you were using file[0] instead of file实际上,现在我又读了一遍——你的例子唯一的问题是你使用的是file[0]而不是file

 <input type="file" id="inputfile" /> <p id="output"></p> <script> var fileInput = document.getElementById('inputfile'); fileInput.addEventListener('change', function(e) { var file = fileInput.files[0]; var reader = new FileReader(); reader.onload = function(e) { document.getElementById('output').innerHTML = reader.result.split("\\n").map((val, i) => i%2===0 ? `<h2>${val}</h2>` : `<p>${val}</p>`).join(''); }; reader.readAsText(file); // HERE! }); </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 javascript 文件中的变量显示到 html 页面上 - Displaying a variable from a javascript file onto a html page 如何将Javascript函数的值显示在HTML页面上? - How to display value from a Javascript function onto an HTML page? 将变量从外部javascript文件传递到html中的值? - Passing a variable from an external javascript file onto a value in html? 从 url 获取 JSON 并打印到页面上 - Fetch JSON from a url and print onto page 使用Javascript将HTML注释插入页面 - Inserting HTML comments onto page using Javascript 从.js文件在html页面数据上打印 - Print on html page data from .js file 是否可以通过javascript或python(Flask)将文本文件中的数据打印到html页面? - Is it possible to print the data from a text file to a html page via javascript or python(Flask)? 用嵌套引号将JavaScript脚本从PHP脚本拖到HTML页面上? - Echoing out JavaScript with nested quotes from a PHP script onto an HTML page? 在 JavaScript 中使用 fetch() 时,如何将返回的 object 中的详细信息渲染到 html 页面上 - When using fetch() in JavaScript how do I render details from the returned object onto the html page 我正在尝试将 append 图像从 javascript object 数组放到我的 html 页面上 - I'm trying to append an image from a javascript object Array onto my html page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM