如何通过jQuery将PHP文件加载到DIV中?

我正在开发一个网站,我在侧面菜单上有这个:

Contact Us

然后我有这个脚本

 $(document).ready(function(){ $("#contact").click(function(){ $("#contents").load('home.php'); }); }); 

我在我的页面中有这个DIV:

显然,我想要做的是在我点击Contact Us超链接时加载home.php,这不起作用。 我的代码出了什么问题?

添加home.php页面url而不是文件名。

 $(document).ready(function(){ $("#contact").click(function(){ $("#contents").load('url to home.php'); }); }); 

您可以像这样使用$.load()来获取更多有关发生的数据。 当您看到错误消息时,您可能可以自己解决它^^

 $("#contents").load("home.php", function(response, status, xhr) { if (status == "error") { // alert(msg + xhr.status + " " + xhr.statusText); console.log(msg + xhr.status + " " + xhr.statusText); } }); 

@ user2805663我知道这篇文章已经很老了但是让我发布解决方案它可能会帮助别人,因为它帮助了我。

感谢@Mangala Edirisinghe

通过以下方法,您可以通过单击(链接)在两个不同的DIV中加载两个单独的文件。

 $(document).ready(function(){ $("#clickableLink").click(function(){ $("#contents").load('url/file1.php'); $("#contents2").load('url/file2.php'); }); }); 

您必须使用索引目录中的“ home.php ”路径而不是脚本目录。

如果您的网站是:

 / index.php scripts /script.js /home.php 

你必须修改你的参数传递“ scripts / home.php