Jquery 在滚动页面时加载内容的Ajax请求
我试了又找,但没有找到。。。如何更改我编写的用于on()方法的以下方法 提前谢谢 丹尼斯 更新1 我将代码更改为以下内容,但是动态创建的东西没有功能-我是否缺少一些静态引用Jquery 在滚动页面时加载内容的Ajax请求,jquery,ajax,Jquery,Ajax,我试了又找,但没有找到。。。如何更改我编写的用于on()方法的以下方法 提前谢谢 丹尼斯 更新1 我将代码更改为以下内容,但是动态创建的东西没有功能-我是否缺少一些静态引用 $(window).on('scroll',function(){ if($(window).scrollTop()==($(document).height()-$(window).height())){ //Get older posts $.ajax({ type: 'POST',
$(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);
}
});
}
});
更新2
动态创建的元素缺少以下功能:
$('#postsDiv').on('keydown', '.commenttext', function(e) {
if ((e.which == 13) && !e.shiftKey) {
comment($(this));
return false;
}
});
方法comment()如下所示:
//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();
}
}
//对帖子进行评论的函数
函数注释(commenttext)
{
//发帖
var commenttextid=commenttext.attr('id');
var postid=commenttextid.replace(“commenttext”,“commenttext”);
//获取评论文本
var commenttext=$('#commenttext'+postid).val();
//commenttext的Ajax不是空的
如果(commenttext!=“”)
{
$.ajax({
键入:“POST”,
url:'action/comment.php',
数据:“commenttext=”+commenttext+”&postid=”+postid,
成功:功能(数据){
//前置评论
$('#注释'+posted).hide().prepend(
“
”+commenttext.replace(/\n/g“
”)+”1)几天前我编写了一段类似的代码。在这里,我将页面滚动事件分配给一个函数,该函数检查用户离底部的像素数。如果用户的像素数少于300,则会触发loadMoreArticles()函数:
$(document).scroll(function(){
if(($(document).height()-$(window).height()-$(document).scrollTop()) < 300){
console.log('Scrolled to bottom');
ArticleList.loadMoreArticles();
} else {
console.log('Scroll '+$(document).height()+' - '+$(window).height()+' - ' +$(document).scrollTop());
}
});
$(窗口)。在(“滚动”,函数(事件){…});
这些东西没有什么样的功能?@VisioN新加载的内容有文本区域,用户在输入时应该能够提交文本(对新内容进行评论)-另请参见问题编辑2@VisioN你知道吗?我是不是在on()中缺少选择器方法?必须将该功能再次添加到所有$(“#postsDiv”)对象。因为它只将功能应用于当前加载的内容divs@Wezelkrozum谢谢,这很好,因为在到达窗口末尾前不久,我还在寻找加载新内容的方法!我对jQuery还是新手,很好奇:console.log()是什么是吗?我的建议是在“结尾”处添加一个隐藏元素。如果距离屏幕顶部x%,请添加更多文章。当您在Chrome中查看网页时,可以按CTRL+SHIFT+J并打开开发者控制台。单击控制台,您可以使用控制台查看javascript生成的所有消息。log()这是调试javascript代码的完美方法。但请注意!在IE的console.log()中如果开发人员工具不可见,该命令将停止javascript的运行。您可以按F12来显示它们。顺便说一句,我构建它的网页是没有功能的?通过ajax加载的任何内容都不会绑定jquery事件等,除非您使用.on()方法来附加它们。新加载的内容没有功能。我正在加载人们应该能够对其进行评论的帖子,即使它们是动态创建的。我在on()方法中使用ajax,所以我想知道为什么它们仍然缺少功能…我试过了,但没有效果。然后用户无法再对任何内容进行评论。
$(window).on('scroll', function(){
// ...
});
$(document).scroll(function(){
if(($(document).height()-$(window).height()-$(document).scrollTop()) < 300){
console.log('Scrolled to bottom');
ArticleList.loadMoreArticles();
} else {
console.log('Scroll '+$(document).height()+' - '+$(window).height()+' - ' +$(document).scrollTop());
}
});
$(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);
//Remove all functionalities
$('#postsDiv').off('keydown');
//Add all functionalities again
$('#postsDiv').on('keydown', '.commenttext', function(e) {
if ((e.which == 13) && !e.shiftKey) {
comment($(this));
return false;
}
});
}
});
}
});