Javascript 如何通过jQuery将PHP文件加载到DIV中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12524228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load PHP file into DIV by jQuery?
提问by JudeJitsu
I am developing a website and I have this on the side menu :
我正在开发一个网站,我在侧面菜单上有这个:
<a href="#" class="leftMenu" id="contact">Contact Us</a>
<a href="#" class="leftMenu" id="contact">Contact Us</a>
then I have this script
然后我有这个脚本
$(document).ready(function(){
$("#contact").click(function(){
$("#contents").load('home.php');
});
});
and I have this DIV inside my page :
我的页面中有这个 DIV:
<div class="contentWrapper" id="contents"></div>
<div class="contentWrapper" id="contents"></div>
Obviously, what I am trying to do is to load home.php when I click on the Contact Us hyperlink, which doesn't work. What is wrong with my code?
显然,我想要做的是在单击“联系我们”超链接时加载 home.php,但这是行不通的。我的代码有什么问题?
回答by Mangala Edirisinghe
add home.phppage url instead of file name.
添加home.php页面 url 而不是文件名。
$(document).ready(function(){
$("#contact").click(function(){
$("#contents").load('url to home.php');
});
});
回答by Tom
You can use $.load()like this, to get more data of whats happening. When you see the error message, you probably can solve it yourself ^^
您可以$.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);
}
});
回答by TechYogi
@user2805663 I know this post is pretty old but though let me post the solution it might help someone else, as it helped me.
@ user2805663 我知道这篇文章已经很老了,但尽管让我发布解决方案,但它可能会帮助其他人,因为它帮助了我。
Thanks to @Mangala Edirisinghe
感谢@Mangala Edirisinghe
by following method you can load two separate files in two different DIVs with single click(Link).
通过以下方法,您可以通过单击(链接)在两个不同的 DIV 中加载两个单独的文件。
$(document).ready(function(){
$("#clickableLink").click(function(){
$("#contents").load('url/file1.php');
$("#contents2").load('url/file2.php');
});
});
回答by Domenico Luciani
You have to use the path of "home.php" from index dirand not from script dir.
您必须使用索引目录中的“ home.php”路径,而不是脚本目录中的路径。
If you site is :
如果您的网站是:
/
index.php
scripts
/script.js
/home.php
You have to modify your parameter passing "scripts/home.php"
你必须修改你的参数传递“ scripts/home.php”

