【问题标题】:how to put a text file in a <textarea>如何将文本文件放入 <textarea>
【发布时间】:2016-04-19 19:24:01
【问题描述】:

有没有办法用textarea 引用 .txt 文件?到目前为止我尝试过的都不起作用我做想使用 php 或 ajax,只是裸 html+css+javascript

  &lt;textarea rows="16" cols="100" name="textdata" href ="hokuco.com/home/livedoc/user.txt"&gt;&lt;/textarea&gt;&lt;br/&gt;

【问题讨论】:

标签: html text textarea


【解决方案1】:

您可以使用 javascript 读取文件(请参阅链接),然后将其添加到 textarea。

https://stackoverflow.com/a/14446538/2550732

编辑:

Javascript:

function readTextFile(file)
{
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, false);
    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
                var allText = rawFile.responseText;
                alert(allText);
            }
        }
    }
    rawFile.send(null);
}

Jquery:

$.ajax({
    url : "helloworld.txt",
    dataType: "text",
    success : function (data) {
        $(".text").html(data);
    }
});

【讨论】:

  • 我不认为这是他们想要的
  • 把这种类型的链接放在Comment
  • 你的权利,我想引用我的文本,但也用 php 保存到它
  • 你能用裸 html 完成这个吗?
  • @parseguy:- 不,你不能。
【解决方案2】:

你可以用JQuery试试看

$(".text").load("myText.txt");

查看jQuery.load()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多