Javascript 将函数应用于每个div
所以基本上我有n个div,它们保存评论,就像在facebook上一样,在Jquery中我有一个函数,它运行一个ajax调用,获取每个div的评论,至少这是我想要它做的,它只获取页面上第一个div的评论,我如何使函数同时为每个div运行 代码如下:AjaxJavascript 将函数应用于每个div,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,所以基本上我有n个div,它们保存评论,就像在facebook上一样,在Jquery中我有一个函数,它运行一个ajax调用,获取每个div的评论,至少这是我想要它做的,它只获取页面上第一个div的评论,我如何使函数同时为每个div运行 代码如下:Ajax interval = setInterval(function(){ comment_id = $("#main-photo"+k).attr("commentid"); k = $("#main-photo"+k).attr(
interval = setInterval(function(){
comment_id = $("#main-photo"+k).attr("commentid");
k = $("#main-photo"+k).attr("nr_crt");
$.post('../utility/countcomm.php', { comment_id: comment_id } ,
function(output) {
if (+total1 < +output)
total1 = output;
if (+total1 > +total2)
{
$.post('../utility/fetchcomments.php', { comment_id: comment_id, start:total2, end:total1 } ,
function(output1) {
$(".comment_append"+k).append("<p class='comment'>"+output1+"</p>");
var scrolldown = $('.comment_append'+k)[0].scrollHeight;
$('.comment_append'+k).animate({scrollTop:scrolldown}, 200);
});
total2 = total1;
}
});
},100);
interval=setInterval(函数(){
评论id=$(“#主照片”+k).attr(“评论id”);
k=$(“#主照片”+k).attr(“nr#U crt”);
$.post('../utility/countcomm.php',{comment\u id:comment\u id},
功能(输出){
如果(+total1<+输出)
total1=输出;
如果(+total1>+total2)
{
$.post('../utility/fetchcomments.php',{comment\u id:comment\u id,开始:total2,结束:total1},
功能(输出1){
$(“.comment\u append”+k).append(“”+output1+”
”);
var scrolldown=$('.comment_append'+k)[0];
$('.comment_append'+k).animate({scrollTop:scrolldown},200);
});
total2=total1;
}
});
},100);
HTML:
"/>
每个div在php中动态分配了不同的id。
谢谢。当我正确阅读/理解您的代码时,您希望查找带有类主照片的所有图像,获取commentid并更新相关div?在这种情况下,您需要对该类的所有图像进行循环:
interval = setInterval(function(){
// loop over all images with the class
$(".main-photo").each( function() {
// get the comment_id and k (the unique part of the id)
var $element = $(this),
comment_id = $element.attr("commentid"),
k = $element.attr("nr_crt");
// the rest is unchanged...
$.post('../utility/countcomm.php', { comment_id: comment_id } ,
function(output) {
if (+total1 < +output)
total1 = output;
if (+total1 > +total2)
{
$.post('../utility/fetchcomments.php', { comment_id: comment_id, start:total2, end:total1 } ,
function(output1) {
$(".comment_append"+k).append("<p class='comment'>"+output1+"</p>");
var scrolldown = $('.comment_append'+k)[0].scrollHeight;
$('.comment_append'+k).animate({scrollTop:scrolldown}, 200);
});
total2 = total1;
}
});
});
},100);
interval=setInterval(函数(){
//与类一起循环所有图像
$(“.main photo”)。每个(函数(){
//获取注释\u id和k(id的唯一部分)
var$element=$(此),
comment_id=$element.attr(“commentid”),
k=$element.attr(“nr_crt”);
//其余的不变。。。
$.post('../utility/countcomm.php',{comment\u id:comment\u id},
功能(输出){
如果(+total1<+输出)
total1=输出;
如果(+total1>+total2)
{
$.post('../utility/fetchcomments.php',{comment\u id:comment\u id,开始:total2,结束:total1},
功能(输出1){
$(“.comment\u append”+k).append(“”+output1+”
”);
var scrolldown=$('.comment_append'+k)[0];
$('.comment_append'+k).animate({scrollTop:scrolldown},200);
});
total2=total1;
}
});
});
},100);
顺便说一句,您应该考虑定期刷新设置(100毫秒)会产生大量服务器请求。根据数据量的不同,这可能会导致一些问题。为所有div设置一个公共类,然后将ajax函数一般分配给所有div。。这可能有助于我进一步了解。。。
interval = setInterval(function(){
// loop over all images with the class
$(".main-photo").each( function() {
// get the comment_id and k (the unique part of the id)
var $element = $(this),
comment_id = $element.attr("commentid"),
k = $element.attr("nr_crt");
// the rest is unchanged...
$.post('../utility/countcomm.php', { comment_id: comment_id } ,
function(output) {
if (+total1 < +output)
total1 = output;
if (+total1 > +total2)
{
$.post('../utility/fetchcomments.php', { comment_id: comment_id, start:total2, end:total1 } ,
function(output1) {
$(".comment_append"+k).append("<p class='comment'>"+output1+"</p>");
var scrolldown = $('.comment_append'+k)[0].scrollHeight;
$('.comment_append'+k).animate({scrollTop:scrolldown}, 200);
});
total2 = total1;
}
});
});
},100);