从外部文件加载div内容

时间:2012-11-06 16:50:22

标签: php jquery load external

我尝试了几个选项,尝试将id =“container”的div上的内容加载到另一个html页面id =“landing”上的div中。

我在这里使用了答案,还有很多变化 Load content of a div on another page

我正在运行测试并且正在加载Jquery。

测试文件在http://www.salcombeyurts.co.uk/test/landing.html

我的代码如下所示:

<script type="text/javascript">
$('#landing').load('source.html #container');
</script>

这最终会在PHP页面上结束。 Joomla网站的一部分。

3 个答案:

答案 0 :(得分:1)

在定义#landing div之前运行脚本。

在DOM ready事件后运行代码

$(function(){
    $('#landing').load('source.html #container');
});

答案 1 :(得分:1)

看起来你的建议是做一个AJAX请求并将整个页面加载到当前页面上的#container div,这不是一个坏主意。另一方面,你似乎想要做的是加载页面,然后在该页面内获取div的内容并将其放在当前页面上的容器div中,这是一个过于复杂和一个糟糕的解决方案问题是什么。

这是我的解决方案

$(function() {
    $.get('page with the div you want', function(data) {
        var content = $(data); //turn the page into a jquery object
        var div = content.find('#div'); // this is the div you want the content from
        $('#container').html(div.html()); // this will set the content of the current div
    });
});

答案 2 :(得分:0)

我会将您的脚本移动到页面底部并将其替换为。

原件:

<script type="text/javascript">
    $('#landing').load('source.html #container');
</script>

新:

<script type="text/javascript">
$(document).ready(function() {

    $('#landing').load('source.html #container');
});
</script>

请注意source.html#container.之间的空格移除

相关问题