使用Ajax/jQuery将外部页面中的内容加载到另一个页面

Eli*_*Eli 6 javascript ajax jquery

有没有办法做到这一点?

page1.php - 有

<div id="main">
   <div id="content">hello</div>
</div>
Run Code Online (Sandbox Code Playgroud)

index.php - 有

<div id="main">
</div>
Run Code Online (Sandbox Code Playgroud)

我可以以某种方式从contentdiv中的page1.php中获取数据并将其加载到main我的index.php中的div中吗?

我已经使用css-tricks网址提供的代码完成了这项工作:http://css-tricks.com/examples/DynamicPage/

但这会使用哈希更改事件.我不想使用哈希特性,只是加载内容功能,但我似乎无法隔离代码,因为我认为它内置于bbq hashchange插件中.

有Ajax方式吗?

就像是

$(selector).find('#main').load('#content');
Run Code Online (Sandbox Code Playgroud)

kar*_*m79 10

只需在.load第一个参数中的URL后面添加一个过滤选择器:

$(document).ready(function() {
    $("#main").load('page1.php #content');
});
Run Code Online (Sandbox Code Playgroud)

这将在当前页面中注入#main div,其中包含来自page1.php的#content内容.