简体   繁体   中英

Applying a javascript code for two different php pages

In my project I have this code who takes results from mysql query and put it into comment DIV and a jquery code who takes me more results when I scroll down my page via a another page code

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

I would like to apply this javascript below for my comment DIV but it works only for the DIVS before I scroll down the page

$('#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;
});

How I can apply this javascrip code for all the DIVs (before scrolling and after scrolling)

Thanks.

Solution 1:

Add your click function to the global scope, if the content is changed reassign:

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

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

Solution 2(even better):

Detect if a parent element was clicked, and than check if it was a confirmdelete element:

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

See: http://api.jquery.com/on/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM