javascript 使用 jquery 附加一个 php 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19169749/
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
Append a php file with jquery
提问by craig
i have a file which echoes out data from a database.
我有一个从数据库中回显数据的文件。
I wish to have a load more button which appends this file so that it will keep loading the rest of the results.
我希望有一个加载更多按钮来附加这个文件,以便它继续加载其余的结果。
The php page works fine but need help with the jquery...
php 页面工作正常,但需要有关 jquery 的帮助...
have used this else where for a json return but dont think this is needed for this.
已经在其他地方使用了这个 json 返回,但不认为这是需要的。
So i am trying this:
所以我正在尝试这个:
$(document).ready(function(){
$("#loadmore").click(function () {
$("#content").append('includes/loadmorebuilds.php');
});
});
In essence, this works but it appends 'includes/loadmorebuilds.php' as just that. I simply appends those words and not the file.
从本质上讲,这有效,但它只是附加了“includes/loadmorebuilds.php”。我只是附加这些词而不是文件。
Any help on this?
有什么帮助吗?
Many thanks!
非常感谢!
回答by pveyes
You could use $.ajaxto get content from file to be appended into DOM. One important thing is that you should use Relative PATH to your web rooton url parameter in $.ajax
您可以使用$.ajax从文件中获取要附加到 DOM 中的内容。一件重要的事情是您应该在 url 参数上使用相对路径到您的网络根目录$.ajax
So it will become like this
所以会变成这个样子
$('#loadmore').click(function() {
$.ajax({
url: '/relative/path/to/your/script',
success: function(html) {
$("#content").append(html);
}
});
});
And make sure you should be able to access your script on http://www.example.com/relative/path/to/your/script
并确保您应该能够在http://www.example.com/relative/path/to/your/script上访问您的脚本
回答by Jason P
You have two options:
您有两个选择:
$('#content').load('includes/loadmorebuilds.php');
Which will replace the content of #contentwith the new html.
这将用#content新的 html替换 的内容。
Or this:
或这个:
$.ajax({
url: 'includes/loadmorebuilds.php'
}).done(function(data) {
$('#content').append(data);
});
Which will append the new data.
这将附加新数据。
回答by Osama Jetawe
use $.ajax
使用$.ajax
$(document).ready(function(){
$(".loader").click(function(){
$.ajax({
url:"index.php",
dataType:"html",
type:'POST',
beforeSend: function(){
},
success:function(result){
$(".content").append(result);
},
});
});
});

