2013-02-02 3 views
-1

기본적으로 페이스 북과 마찬가지로 div가있는 div가 있고 Jquery에는 모든 div에 대한 주석을 가져 오는 Ajax 호출을 실행하는 함수가 있습니다. 적어도 원하는 것은 그 것입니다. 하려면, 그것은 단지 페이지에서 첫 번째 div에 대한 의견을 가져옵니다, 어떻게 각 div 동시에 기능을 실행하게합니까? 여기 각 div에 함수 적용

코드입니다 : 아약스

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); 

HTML :

<div id="comment_box"> 
    <input type="text" name="comment" id="type_comment" value="Type a comment." /> 
    <div id="comment_append" class="comment_append<?php echo $k; ?>"> 

    </div><!--comment_append end--> 
    <img id="main-photo<?php echo $k; ?>" nr_crt="<?php echo $k; ?>" class="main-photo" src="<?php echo $user_uploads['pic1'] ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" commentid="<?php echo $user_uploads['comment_id']; ?>"/> 
</div><!--comment_box end--> 

모든 사업부가 PHP에서 동적으로 할당 된 다른 ID가 있습니다. 감사합니다.

+3

모든 div에 공통된 클래스를 넣은 다음 일반적으로 모든 div에 Ajax 기능을 할당하십시오. 이것이 도움이 될 수도 있습니다. – sourcecode

+0

더 자세히 보도록하겠습니다 ... – southpaw93

답변

1

코드를 올바르게 읽고 이해하면 클래스 사진이있는 모든 이미지를 찾고 commentid를 가져 와서 관련 div를 업데이트하고 싶습니까? 당신이 당신의주기적인 갱신 설정 (100 밀리)에 대해 생각해야 서버 요청의 엄청난 금액을 생산합니다

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); 

그런데 :이 경우 해당 클래스의 모든 이미지를 통해 루프가 필요합니다. 데이터 양에 따라 몇 가지 문제가 발생할 수 있습니다.

+0

놀라운 사람 작동 : D 감사합니다. – southpaw93

관련 문제