Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.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 通过jquery追加HTML文件失败_Javascript_Jquery_Html - Fatal编程技术网

Javascript 通过jquery追加HTML文件失败

Javascript 通过jquery追加HTML文件失败,javascript,jquery,html,Javascript,Jquery,Html,我知道这个问题已经被问了好几次,但我遇到了这个问题。 我想用otherpage.html的内容附加index.html文件 我的index.html代码是 <html> <head> <script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script> <script type="text/javascript">

我知道这个问题已经被问了好几次,但我遇到了这个问题。 我想用otherpage.html的内容附加index.html文件

我的index.html代码是

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $.get("otherpage.html", function (data) {
                $("#appendToThis").append(data);
            });
        });
    </script>
</head>
<body>
    <div id="appendToThis"></div>
</body>

$(函数(){
$.get(“otherpage.html”,函数(数据){
$(“#追加此”)。追加(数据);
});
});

我的otherpage.html代码是

<p>This is second page.</p>
这是第二页


当我打开Index.html时,我看到一个空白的白色页面。任何人都可以指出错误所在。。提前谢谢。

在网站或本地主机上运行您的文件。通过文件协议在chrome浏览器中禁用Ajax请求


如果您真的想运行它,可以尝试以下标志:
chrome——允许从文件访问文件

<script type="text/javascript">
    $(function () {
       $("#appendToThis").load("otherpage.html");
    });
 </script>

$(函数(){
$(“#appendToThis”).load(“otherpage.html”);
});

这是另一个我认为正是您所寻找的答案:


这个例子使用了其他人在这个线程中提出的一些观点。JS需要一个AJAX请求来读取文件、解析文件,然后替换该信息。该示例使用onClick方法,但您应该能够使用任何喜欢的方法(包括加载)来执行定义的AJAX调用函数。

您的代码没有问题,问题可能是:

  • 默认情况下,浏览器无法通过ajax从系统加载文件,因此您必须在web服务器中运行页面。您可以使用,此服务器易于使用和安装,但首先必须安装
  • 如果您已经在服务器上运行了页面,但仍然无法加载部分页面,则部分页面url可能错误

  • 你不能在文件系统上执行ajax代码对我来说很好。最有可能的是,您遇到了CORS错误。尝试将其加载到web服务器中。控制台是否记录任何错误?