繁体   English   中英

在HTML页面中显示文本文档

[英]Displaying Text Documents in an HTML Page

我一直在尝试创建一个网页,该网页将从同一目录加载.tx文件,并将内容显示为页面上的不同部分。 经过一番搜索之后,我发现了JQuery,它可以让我从任何给定的.txt文档中查找和显示文本,但是,我在另一方面进行了努力。

我希望能够将任何.txt文档添加到页面的文件夹中,并在下次打开页面时显示它,而无需任何额外的代码。 本质上,我将一些文本文件放到该文件夹​​中,它们自动显示会很好。

如果有人可以提供一些建议或告诉我在哪里寻找答案,将不胜感激,因为经过几天的搜索,这开始让我感到沮丧。

谢谢

关于如何从目录加载所有图像以及如何加载文本文件,也存在一些类似的SO问题。 这适用于:

的HTML:

<div id='foundTexts'>
    <ul>
    </ul>
</div>

js:

    var directory = 'texts/';

    $.ajax({
        //This will retrieve the contents of the folder if the folder is configured as 'browsable'
        url: directory,
        dataType: "text",
        success: function (data) {
            //List all txt file names
            $(data).find("a:contains(.txt)").each(function (i) {
                // find the new file url of each txt file
                var newFileLocation = directory+$(this).text().substring(1);

                $.get(newFileLocation, function(response) {
                    // loads the content of each file, inserts into a list item and appends to an unordered list
                    $("#foundTexts ul").append( '<li>'+ response +'</li><br>');
                });
            });
        }
    });

确保将服务器文件夹配置为可浏览,并且已将directory变量设置为文本文件的正确文件夹位置。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM