Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将文本文件动态读入页面_Javascript_Html - Fatal编程技术网

Javascript 将文本文件动态读入页面

Javascript 将文本文件动态读入页面,javascript,html,Javascript,Html,我正在尝试将一个.txt文件加载到我的简单html页面中。我很不爽,我所有的密码都是从stackoverflow偷来的 文本文件与html文件位于同一文件夹中,包含一些我希望在div中显示的文本,不仅在开始时加载,而且动态更新(间隔1秒) 代码生成的页面只是空的。 我用的是Chrome73 我只使用html文件和txt文件。文件夹中没有其他文件 <html> <head> <script type="text/javascript"> setInt

我正在尝试将一个.txt文件加载到我的简单html页面中。我很不爽,我所有的密码都是从stackoverflow偷来的

文本文件与html文件位于同一文件夹中,包含一些我希望在div中显示的文本,不仅在开始时加载,而且动态更新(间隔1秒)

代码生成的页面只是空的。 我用的是Chrome73

我只使用html文件和txt文件。文件夹中没有其他文件

<html>
 <head>
  <script type="text/javascript">
   setInterval(read,1000);
   function read(){
    jQuery.get('file.txt',function(data){$('#container').html(data);});
   }
   read();
  </script>
 </head>
 <body>
  <div id="container"></div>
 </body>
</html>

设置间隔(读取,1000);
函数read(){
get('file.txt',函数(数据){$('#容器').html(数据);});
}
read();

我不知道这个代码有什么问题。我错过图书馆了吗?如果您提出了一个全新的代码,我们也会非常感激。

是的,您缺少jQuery库。像这样试试,让我知道:

<html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
        <script type="text/javascript">
            function read(){
                jQuery.get('file.txt',function(data){$('#container').html(data);});
                setTimeout(function(){read() },1000);
            }
            read();
        </script>
    </head>
    <body>
        <div id="container"></div>
    </body>
</html>

函数read(){
get('file.txt',函数(数据){$('#容器').html(数据);});
setTimeout(函数(){read()},1000);
}
read();
参考:


简单的jQuery加载怎么样

$("#container").load("file.txt");

仍然不起作用,同样的结果是,在edge中运行它的空白页面可以正常工作,但在chrome中则不行,这无疑是一个进步