2014-02-15 2 views
0

나는이 방법을 일부 아약 않습니다 및 div 결과를 새 div 전에 만든 다음 ajax 응답을 삽입합니다. 내가 가진 문제는 두 번째 클릭을 사용하면 이전에 만든 div (내가 원하는 것) 전에 새 div를 추가하지만 다른 div는 (정확히 내가 원하는 것이 아닌) 만듭니다. 정확히 똑같습니다.클릭하여 사용하여 jQuery()

AJAX/jQuery를 :

<div class='commentsContaier'> 
    <div class="3">stuff in here</div> 
    <div class="2">stuff in here</div> 
    <div class="3">stuff in here</div> 
    <div class="1">stuff in here</div> 
</div> 

답변

0

때문에 $('div[data="' + currentDiv + '"] .commentsContainer > div') 일치하는 모든 <div>을 : 두 번째 클릭 (BAD ONE)의

<div class='commentsContaier'> 
    <div class="2">stuff in here</div> 
    <div class="1">stuff in here</div> 
</div> 

HTML 결과 : 첫 번째 클릭의

$(document).on('click', '.ajaxcommentbutton1', function() { 
var currentDiv, pageValue, newPage, newPageid; 
currentDiv = $(this).attr('group'); 
pageValue = parseFloat($(this).attr('data')); 
newPage = pageValue + 1; 
newPageid = '.' + newPage; 

$.ajax({url:"comments.php", 
     type:'POST',    
    dataType:'text', 
     data: {id: currentDiv, 
       page: newPage}, 
    success:function(result){ 
     $('div[data="' + currentDiv + '"] .commentsContainer > div').before("<div class='" + newPage + "'>&nbsp</div>"); 
     $('div[data="' + currentDiv + '"] ' + newPageid).html(result); 
     $('button[group="' + currentDiv + '"]').attr('data', newPage); 
     $('button[group="' + currentDiv + '"]').off(); 
    }}); 
}); 

HTML 결과 귀하의 컨테이너 아래에 .before()이 모든 AJAX 콘텐츠가 일치하는 <div>에 포함되어있어 AJAX 콘텐츠를 복제 할 수 있습니다.

는 두 번째 클릭에 새 요소는 :first-child 선택으로 모든 <div>의의 첫 번째 자식을 일치하도록 $('div[data="' + currentDiv + '"] .commentsContainer > div:first-child')로 교체 시도 모두 <div class="1">stuff in here</div><div class="2">stuff in here</div>

에 삽입 즉

+0

감사합니다 ! 엄지 손가락 – donny5561

관련 문제