我试过并搜索过,但没有找到...如何更改我编写的使用on()方法的以下方法?Ajax请求在滚动页面时加载内容
//Get old posts when scrolling down
$(window).scroll(function(){
if($(window).scrollTop()==($(document).height()-$(window).height())){
//Get older posts
$.ajax({
type: 'POST',
url: 'action/getoldposts.php',
success: function(oldposts){
//Append #postsDiv
$('#postsDiv').append(oldposts);
}
});
}
});
在此先感谢!
丹尼斯
更新1
我改变了代码以下,但随后被动态创建不具备的功能的东西 - 我失去了一些静态的参考?
$(window).on('scroll',function(){
if($(window).scrollTop()==($(document).height()-$(window).height())){
//Get older posts
$.ajax({
type: 'POST',
url: 'action/getoldposts.php',
success: function(oldposts){
//Append #postsDiv
$('#postsDiv').append(oldposts);
}
});
}
});
UPDATE 2
动态创建元素错过了以下功能:
$('#postsDiv').on('keydown', '.commenttext', function(e) {
if ((e.which == 13) && !e.shiftKey) {
comment($(this));
return false;
}
});
方法评论()如下所示:
//Function to comment on a post
function comment(commenttext)
{
//Get postid
var commenttextid=commenttext.attr('id');
var postid=commenttextid.replace("commenttext", "");
//Get commenttext
var commenttext=$('#commenttext'+postid).val();
//Ajax of commenttext is not empty
if(commenttext!="")
{
$.ajax({
type: 'POST',
url: 'action/comment.php',
data: 'commenttext='+commenttext+'&postid='+postid,
success: function(data){
//Prepend comments
$('#comments'+postid).hide().prepend(
'<div class="comment"><hr/>'+commenttext.replace(/\n/g, '<br/>')+'</div'
).fadeIn('slow');
//Remove from textarea what was typed in
$('#commenttext'+postid).val('');
//Focus on textarea
$('#commenttext'+postid).focus();
}
}).error(function(){
alert('The comment could not be sent - please try again later.');
});
}
else
{
//If the commenttext is empty, focus on the textarea nonetheless
$('#commenttext'+postid).focus();
}
}
注释被所附到新加载的内容,但显然错过了Ajax,因为当我重新加载页面它不是那里了。
'$(窗口)。在( “滚动”,函数(事件){...});' – VisioN
的东西没有什么样的功能? – VisioN
@VisioN新加载的内容有文本区域,在输入用户应该能够提交文本(评论新内容) - 请参阅问题编辑2 – weltschmerz