Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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
使用JQuery.load从另一个页面加载div_Jquery_Html - Fatal编程技术网

使用JQuery.load从另一个页面加载div

使用JQuery.load从另一个页面加载div,jquery,html,Jquery,Html,我正在尝试学习JQuery,但在完成一项简单的任务时遇到了困难。我有一个页面(dir:root/admin/indexContent.html),我想从另一个页面(dir:root/index.html)加载ID为#header1的h2标记的内容 以下是indexContent.html头部的脚本 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

我正在尝试学习JQuery,但在完成一项简单的任务时遇到了困难。我有一个页面(dir:root/admin/indexContent.html),我想从另一个页面(dir:root/index.html)加载ID为#header1的h2标记的内容

以下是indexContent.html头部的脚本

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
     $("#indexHeader1").load("../index.html #header1");
});
</script>
再加上几行
GEThttp://site/admin/images/whatever.png
。我不明白它为什么要得到这些图像,我只想要一个H2元素。同样值得注意的是,这些图像的路径是错误的

http://highstreetdeliwb.com/admin/images/navigation/logoClear.png
应该是:

http://highstreetdeliwb.com/images/navigation/logoClear.png

您的代码似乎是正确的,但您是否尝试过使用ajax()函数从另一个页面获取内容?因为你必须得到一个标签,你可以很容易地附加它

$.ajax({
url: '../index.html',
dataType: 'html',
success: function(html) {
    var div = $('#header1', $(html)).addClass('done');
        $('#indexHeader1').html(div);
    } 
});
试试看

编辑
我已经用你的ID和我在评论中提到的链接修改了代码。

你做了什么来调试它?您是否查看过浏览器的JS控件以查看是否存在任何错误?您是否查看了Net选项卡以查看请求是否正确发送并得到正确响应?您是否添加了一些
console.log
语句以查看代码是否正在执行?您是否检查过正在查找的元素(例如,使用
console.log($(“#indexHeader1”).length)
?代码没有问题。请确保您是从服务器(例如:apache.)运行页面。直接打开html文件在某些浏览器(如firefox)中无法工作我刚刚用更多信息更新了我的帖子。看起来我的路径有点错误。不过,就测试而言,尝试用index.html的完全限定路径替换“./index.html#header1”。它显示为“admin”是根据ajax代码的运行位置添加的,而不是参照index.html的位置。@allinking,我只是尝试了一下,但没有效果。控制台显示它仍在搜索admin/images中的文件。这实际上使我的页面加载了一些内容!不幸的是,它加载了整个页面(文本内容、损坏的图像)。知道为什么它返回整个index.html而不仅仅是ID为header1的H2元素吗?也许正确的答案在这个链接上:。我将为您更新代码。是的!这很有效!有趣的是,我的控制台仍然显示这些图像错误,但我想这并不重要,因为我不会弄乱这些图像。谢谢唐!很高兴听到这个消息,伙计!祝你好运,你剩下的代码!
http://highstreetdeliwb.com/admin/images/navigation/logoClear.png
http://highstreetdeliwb.com/images/navigation/logoClear.png
$.ajax({
url: '../index.html',
dataType: 'html',
success: function(html) {
    var div = $('#header1', $(html)).addClass('done');
        $('#indexHeader1').html(div);
    } 
});