简体   繁体   English

如何从根文件夹中读取文本文件并将其显示在div中?

[英]How to read a text file from the root folder and display it in div?

Hi I have a complicated html page. 嗨,我有一个复杂的HTML页面。 I want to read a text file and display its content into a div on Page load. 我想读取一个文本文件,并将其内容显示在Page load上的div中。 I can use javascript if needed. 如果需要,我可以使用javascript。 Pl. PL。 help. 救命。

Your file is on your server in California. 您的文件位于加利福尼亚州的服务器上。 JavaScript is running on the user's computer in Tokyo. JavaScript正在东京的用户计算机上运行。

How is the user's computer supposed to read content from your server? 用户的计算机应该如何从服务器读取内容? It cannot. 这不可以。 You need to use a server-side language (PHP, Ruby, Perl, ASP, etc.) on your server to dynamically compose the HTML to send to the user. 您需要在服务器上使用服务器端语言(PHP,Ruby,Perl,ASP等)来动态组合HTML以发送给用户。

What server are you using? 你用的是什么服务器?

You could use the jQuery $.ajax() method to load your text file. 您可以使用jQuery $ .ajax()方法加载文本文件。

$.ajax({
  url: '<enter text file url here>',
  dataType: "text",
  success: function(data){
     //load content to div
     $("#<divId>").html(data);
  }
});

Why not do it on the server? 为什么不在服务器上这样做?

<?php
echo '<div>' . file_get_contents('yourtextfile.txt') . '</div>';
?>

why couldn't he use jQuery or something to do an AJAX request? 为什么他不能使用jQuery或其他东西来做一个AJAX请求? if the text file is on the server it should work alight. 如果文本文件在服务器上,它应该工作。

$(function(){
    $('div').load('path_to/textfile.txt', function(){
        alert('data was loaded');
    });
});

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

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