如何使用jquery将外部页面加载到div中?

时间:2010-08-23 08:28:41

标签: php jquery ajax html

我有一个包含3个链接的菜单,当用户点击菜单链接时,我希望页面使用jquery加载到同一页面上的div上,即时通讯使用php和mysql!

谢谢你!

6 个答案:

答案 0 :(得分:3)

http://api.jquery.com/load/

$('#result').load('ajax/test.html');

答案 1 :(得分:1)

$('#local_container').load('external/file.php #external_container');

http://api.jquery.com/load/

答案 2 :(得分:0)

查看jQuery.get()功能。

答案 3 :(得分:0)

我建议你阅读一些文档@ jQuery

顺便说一句,这可能是你的答案:

$.ajax({
  url: 'ajax/test.html',
  success: function(data) {
    $('.result').html(data);
    alert('Load was performed.');
  }
});

答案 4 :(得分:0)

扩展自@ jAndy的例子:

标记:

<a href="/foo/bar.html" id="baz">Load external html</a>
<div id="result"></div>

的JavaScript

$('#baz').click(function () { $('#result').load(this.href); });

答案 5 :(得分:0)

您可以使用ajax从mysql数据库中收集信息。

$.ajax({
  type: 'post',
  url: 'getnames.php',
  datatype: "json",
  success: function(data) {
    $('#id_of_the_div').text("");
    $('#id_of_the_div').append(data.returned);
  }
});

在php文件中:

//..
// commands that run mysql queries and gather information from database
//..
//..
//..
// you store what you've got from the database in a $ret variable and then:
// $ret can be for example:
// $ret = "<table><tr><td>Peter</td><td>40</td></tr></table>";

$arr = Array("returned"=>$ret);
echo json_encode($arr);

当然,这只是您可以使用的几种方式的示例。我已经使用了ajax请求和json ......