Javascript JQuery函数在文档加载时未执行
我正在尝试建立一个简单的社交媒体网站,人们可以在其中发表帖子和评论(有点像facebook)。为了获取帖子和评论,我创建了两个简单的PHP脚本,以json格式获取结果。然后,我在jquery中创建了两个函数,Javascript JQuery函数在文档加载时未执行,javascript,php,jquery,ajax,facebook,Javascript,Php,Jquery,Ajax,Facebook,我正在尝试建立一个简单的社交媒体网站,人们可以在其中发表帖子和评论(有点像facebook)。为了获取帖子和评论,我创建了两个简单的PHP脚本,以json格式获取结果。然后,我在jquery中创建了两个函数,getPosts()和getComments(),用于ajax结果。成功后,这些函数clone()为返回的每个post/comment对象创建的html框架 这是我的html框架:- <div class="KK-posts_feed"> <
getPosts()
和getComments()
,用于ajax结果。成功后,这些函数clone()
为返回的每个post/comment对象创建的html框架
这是我的html框架:-
<div class="KK-posts_feed">
<div class="KK-post_frame" style="display:none;"> //This is the div that I clone() for postings
<div class="KK-post_info_header">
<a class="KK-post_username"></a>
</div>
</div>
<div class="KK-post_text"></div>
<div class="KK-post_comment_thread" data-PID="">
<div class="KK-post_comment_box"> //This is the div that I clone for comments
<div class="KK-post_comment_user_info">
<a class="KK-post_comment_username"></a>
</div>
<div class="KK-post_comment_text"></div>
</div>
</div>
</div>
</div>
这是用于在每个帖子下输出评论的getComments()
方法:-
function getComments(){
$('.KK-post_comment_thread').each(function(){ // I run this method for each comment thread div inside each of my posts div
var pid = $(this).attr('data-PID'); // PID is the 'post id' that I use, to identify each comment thread div separately.
var self = this;
$.ajax({
type : 'POST',
url : 'fetchcomments.php',
data : 'pid='+pid, // I send the post id to the php script which returns the comments for the particular post in the form of json objects
error : function(){
$(self).html('<p class="KK-post_comment_load_error">' + "Sorry, couldn't load comments at the moment." + ' <a class="KK-post_comment_load_retry" href="#">Retry</a>' + "</p>");
},
success : function(data){
var comments = data.comments;
if(comments.length)
{
$('.KK-post_comment_box:first').css('display', 'block');
$(comments).each(function(index, value){
$('.KK-post_comment_box:first')
.clone(true)
.attr('id', value.comment_id)
.find('.KK-post_comment_username').html(value.user_fullname)
.end()
.find('.KK-post_comment_text').html(value.comment_text)
.end()
.appendTo(self);
});
$('.KK-post_comment_box:first').css('display', 'none');
}
}
});
});
}
当getPosts()
方法完美执行并输出帖子时,getComments()
方法要么不执行,要么不显示任何结果。我不能肯定,因为每次我刷新页面时,我只会收到帖子,而不会收到评论。我还检查了我的控制台,但没有发现脚本有任何错误。当我通过类似于$('button')的click事件处理程序执行getComments()
方法时,它工作得非常好代码>。因此,我知道我的两个方法都工作得很好,只是我无法在页面加载时一个接一个地执行它们。有人能帮我解决这个特殊的问题吗 您的getPosts()
函数没有返回任何内容,因此您将未定义的传递给$。when()
您需要返回从$.ajax()返回的内容。
:
多亏了@Pointy,我解决了这个问题,现在问题解决了。我现在意识到我正在向$发送一个未定义的参数。当时,我所要做的就是向我的getPosts()
script添加“return”语句,现在我的脚本工作得很好。再次感谢@Pointy!
function getComments(){
$('.KK-post_comment_thread').each(function(){ // I run this method for each comment thread div inside each of my posts div
var pid = $(this).attr('data-PID'); // PID is the 'post id' that I use, to identify each comment thread div separately.
var self = this;
$.ajax({
type : 'POST',
url : 'fetchcomments.php',
data : 'pid='+pid, // I send the post id to the php script which returns the comments for the particular post in the form of json objects
error : function(){
$(self).html('<p class="KK-post_comment_load_error">' + "Sorry, couldn't load comments at the moment." + ' <a class="KK-post_comment_load_retry" href="#">Retry</a>' + "</p>");
},
success : function(data){
var comments = data.comments;
if(comments.length)
{
$('.KK-post_comment_box:first').css('display', 'block');
$(comments).each(function(index, value){
$('.KK-post_comment_box:first')
.clone(true)
.attr('id', value.comment_id)
.find('.KK-post_comment_username').html(value.user_fullname)
.end()
.find('.KK-post_comment_text').html(value.comment_text)
.end()
.appendTo(self);
});
$('.KK-post_comment_box:first').css('display', 'none');
}
}
});
});
}
$('document').ready(function(){
$.when(getPosts()).done(function(){
getComments();
});
});
function getPosts(){ //This function gets the posts and clones the html for each post
return $.ajax({
// ...