简体   繁体   English

如何在JavaScript中从纯文本读取/写入

[英]How can I read/write from a plain text in JavaScript

I don't know much about JS, I'm new in this world, I know about HTML and CSS but this time I was asked to make an aplication where I have to receive data from a plain text to my formulary using JS. 我对JS不太了解,我是这个世界的新手,对HTML和CSS也不了解,但是这次我被要求做一个应用,我必须使用JS从纯文本接收数据到我的配方中。

Here's my Plain Text data.txt: 这是我的纯文本data.txt:

Interfaces: test1,
IP: 192.168.1.1,
Mask : test,
Gateway : test,
DNS 1: test,
DNS 2: test,
Broadcast: test

Here's my Div: 这是我的Div:

<div class="col-md-12">
            <div class="form-panel">
            <h4><i class="fa fa-angle-right"></i> Formulario </h4>
            <hr />
            <form method="post">
                <div class="form-group">
                    <label class="col-sm-3 col-sm-3 control-label">Interfaces:</label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" 
                    </div>      
                </div>  
                 <div class="form-group ">
                    <label class="col-sm-3 col-sm-3 control-label">IP: </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control"
                    </div>
                </div>
                 <div class="form-group">
                    <label class="col-sm-3 col-sm-3 control-label">Mask : </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" 
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 col-sm-3 control-label"> Gateway : </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" 
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 col-sm-3 control-label">DNS 1 : </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" 
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 col-sm-3 control-label">DNS 2 : </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" 
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 col-sm-3 control-label">Broadcast : </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" 
                    </div>
                </div>
                <br><br>
                <div class="form-group">
                    <button type="submit" name="Save" class="btn btn-success btn-sm" " title="Save"><i class="fa fa-save"></i> Save</button>
                </div>
            </form>
        </div>

That's the script I got from you guys, because I was looking for code to do it and that's what I got. 那是我从你们那里得到的脚本,因为我一直在寻找代码来做,这就是我得到的。

The point is the data that contains "data.tx" should be on my formulary and if I modify any field there and I hit "save" button it has to write on my plain text as well. 关键是包含“ data.tx”的数据应位于我的配方中,如果我在那里修改任何字段,然后单击“保存”按钮,它也必须写在我的纯文本上。

Is there a way to make it work? 有办法使它起作用吗? thanks! 谢谢!

Entire code bellow. 整个代码如下。

 var mytext; var connection = new XMLHttpRequest(); connection.open('GET', '/data.txt'); connection.onreadystatechange = function() { mytext=connection.responseText; } connection.send(); 
 <div class="col-md-12"> <div class="form-panel"> <h4><i class="fa fa-angle-right"></i> Formulario </h4> <hr /> <form method="post"> <div class="form-group"> <label class="col-sm-3 col-sm-3 control-label">Interfaces:</label> <div class="col-sm-9"> <input type="text" class="form-control" </div> </div> <div class="form-group "> <label class="col-sm-3 col-sm-3 control-label">IP: </label> <div class="col-sm-9"> <input type="text" class="form-control" </div> </div> <div class="form-group"> <label class="col-sm-3 col-sm-3 control-label">Mask : </label> <div class="col-sm-9"> <input type="text" class="form-control" </div> </div> <div class="form-group"> <label class="col-sm-3 col-sm-3 control-label"> Gateway : </label> <div class="col-sm-9"> <input type="text" class="form-control" </div> </div> <div class="form-group"> <label class="col-sm-3 col-sm-3 control-label">DNS 1 : </label> <div class="col-sm-9"> <input type="text" class="form-control" </div> </div> <div class="form-group"> <label class="col-sm-3 col-sm-3 control-label">DNS 2 : </label> <div class="col-sm-9"> <input type="text" class="form-control" </div> </div> <div class="form-group"> <label class="col-sm-3 col-sm-3 control-label">Broadcast : </label> <div class="col-sm-9"> <input type="text" class="form-control" </div> </div> <br><br> <div class="form-group"> <button type="submit" name="Save" class="btn btn-success btn-sm" " title="Save"><i class="fa fa-save"></i> Save</button> </div> </form> </div> </div> 

I don't know ia more efficient solution exists, but below code is working. 我不知道存在更有效的解决方案,但是下面的代码正在工作。

Here is how I achieved it, using jQuery .load() 这是我使用jQuery .load()

$(document).ready(function(){
    // Load the file in an hidden div element.
    $("#hiddenFileLoad").load("data.txt", function(){
        // Callback executes when content is loaded

        // Place the content in a var
        var loadedText = $("#hiddenFileLoad").text();
        console.log("loadedText:\n\n"+loadedText);

        // Split into an array
        var loadedTextSplitted = loadedText.split(",");

        // Remove the label part for each
        for (i=0;i<loadedTextSplitted.length;i++){
            temp = loadedTextSplitted[i].split(": ");
            loadedTextSplitted[i] = temp[1];
        }

        // Place each info in the HTML form
        $(".form-panel").find("input").each(function(index){
            $(this).val( loadedTextSplitted[index] );
        });

    }); // End of .load callback
});

I used and hidden div , in order to load the .txt file content, which I gave the "hiddenFileLoad" id . 为了加载.txt文件的内容,我使用了hidden div ,并为其指定了“ hiddenFileLoad” id

I made absolutely no change to your HTML (except the hidden div add) and I used your txt file content. 我对您的HTML几乎没有任何更改(隐藏的div add除外),并且使用了您的txt文件内容。

I assume you know how to save to file... I didn't make the save button to work. 我假设您知道如何保存到文件...我没有使“保存”按钮起作用。

Here is a live example on my server. 这是我服务器上的一个实时示例

如何读取文本文件的内容并将其写入<div>使用普通的 JavaScript?</div><div id="text_translate"><p> <strong>问题:</strong>如何从文本文件中读取内容(与 html 页面一起保存在服务器上)并将该内容写入 HTML 页面上的<em>“innerHTML”</em>中?</p><p> <strong>上下文:</strong>我想创建一个用作博客的 static 网站。 但是,我希望只有一个帖子页面,每个帖子都从文本文件中读取并写入页面的主 div。 文本文件中的内容将用所需的 html 标记进行标记,以便它们在写入 div 时像 html 一样 function (但文件本身将保存为<em>.txt</em>文件)</p><p> 理想情况下,文本文件应该能够存储在与调用它的 html 页面(和/或脚本文件)不同的文件夹中。</p><p> <strong>附加信息:</strong>我遇到的大多数解决方案都与读取用户上传的文件有关。 这不是我想要理解的。 我试图了解如何从作为网站资产一部分存储的文本文件中读取数据。</p></div> - How do I read a text file's content and write it to a <div> using plain JavaScript?

暂无
暂无

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

相关问题 如何读取文本文件的内容并将其写入<div>使用普通的 JavaScript?</div><div id="text_translate"><p> <strong>问题:</strong>如何从文本文件中读取内容(与 html 页面一起保存在服务器上)并将该内容写入 HTML 页面上的<em>“innerHTML”</em>中?</p><p> <strong>上下文:</strong>我想创建一个用作博客的 static 网站。 但是,我希望只有一个帖子页面,每个帖子都从文本文件中读取并写入页面的主 div。 文本文件中的内容将用所需的 html 标记进行标记,以便它们在写入 div 时像 html 一样 function (但文件本身将保存为<em>.txt</em>文件)</p><p> 理想情况下,文本文件应该能够存储在与调用它的 html 页面(和/或脚本文件)不同的文件夹中。</p><p> <strong>附加信息:</strong>我遇到的大多数解决方案都与读取用户上传的文件有关。 这不是我想要理解的。 我试图了解如何从作为网站资产一部分存储的文本文件中读取数据。</p></div> - How do I read a text file's content and write it to a <div> using plain JavaScript? 如何从Javascript读取和编写Angular组件变量? - How can I read and write an Angular component variable from Javascript? 是否可以用Javascript编写纯文本? - Is it possible to write in a plain text in Javascript? 如何在文档中写入 HTML 并将其显示为纯文本,但也可以通过 JavaScript 进行复制? - How do I write HTML in a document and have it be displayed as plain text but also be able to be copied via JavaScript? 如何使用 JavaScript 读取 textarea 中的文本? - How can I read the text in a textarea with JavaScript? 如何将 socket_read 值获取为从 PHP websocket 中的 javascript 发送的纯文本 - how to get socket_read value to plain text sent from javascript in PHP websocket 如何使用javascript从文本文件中读取/写入? - How to Read/Write from text file using javascript? 如何使用javascript读取/写入文本文件? - How to read and write from/into text file using javascript? 如何将javascript对象转换为纯文本,以便在发布数据时我可以发布简单文本 - How to convert javascript object to plain text so that while posting the data i can post simple text 如何使用普通的javascript“鼠标轻扫”? - How can I “mouse swipe” with plain javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM