2016-11-08 43 views
0

在我的項目中,我有這段代碼從mysql查詢中獲取結果,並將其放入註釋DIV和一個jQuery代碼,當我通過另一個頁面向下滾動頁面時需要更多結果代碼爲兩個不同的php頁面應用javascript代碼

<body> 
<div id="container"> 
<div class="comment"> 
    <div id="comm"> 
    </div> 
</div> 
</div> 
<script type="text/javascript"> 

$(document).ready(function(){ 

var offset = $('.comment:last').offset(); 

$(window).scroll(function(){ 
    if((offset.top-$(window).height() <= $(window).scrollTop()) 
    && load==false && ($('.comment').size()>=5) && 
    ($('.comment').size()!=$('.nb_com').text())){ 

     var theme = $('.comment').attr('idtheme'); 
      $.ajax({ 
      url: 'ajax_scroll.php', 
      type: 'get', 
      data: 'theme='+theme, 

      success: function(data) { 

       $('.comment:last').after(data); 
       offset = $('.comment:last').offset(); 

      } 
     }); 
    } 


}); 

}); 

</script> 

我想申請低於我的評論DIV這個JavaScript,但它只是在我向下滾動頁面

$('#confirmdelete a').click(function(){ 

var id_comm=$(this).attr('id'); 
if(confirm("Delete?")) { 
$.ajax({ 
    url: 'commentsdelete.php', 
    type: 'post', 
    async: false, 
    data:{ 
    'id_comm': id_comm 

    }, 
    success:function(){ 

    } 

    }); 
} 
else 
{ 
    }  

return false; 
}); 

我如何應用此的javascrip代碼對所有工程的div DIV(滾動前和滾動後)

謝謝。

+0

將事件偵聽器應用於容器元素,然後返回傳播鏈或重新分配eventlistener,每次添加元素 –

+0

感謝Jonas的幫助,我不是jquery的專業人員。我怎樣才能做到這一點? – simo9900

回答

1

解決方案1:

添加您的點擊功能在全球範圍內,如果內容被改變重新分配:

var onclickfunc=function(){ 
alert("clicked"); 
} 
$('#confirmdelete a').click(onclickfunc); 

//later in your ajax 
sucess:function(data){ 
//add the content 
//reassign: 
    $('#confirmdelete a').click(onclickfunc); 
} 

解決方案2(甚至更好):

檢測一個父元素被點擊,並且檢查它是否是確認刪除元素:

$(document).on("click","#confirmdelete a",function(){ 
//yourcode here 
}); 

請參閱:http://api.jquery.com/on/

+0

感謝您的幫助。第二種解決方案更容易。 – simo9900

相關問題