为两个不同的php页面应用javascript代码

Applying a javascript code for two different php pages

本文关键字:php 应用 代码 javascript 两个      更新时间:2023-09-26

在我的项目中,我有这个代码谁从mysql查询结果,并把它放入评论div和一个jquery代码谁带我更多的结果,当我通过另一个页面代码滚动我的页面

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

我想应用这个javascript下面我的评论DIV,但它只适用于DIV之前,我向下滚动页面

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

如何将此javascript代码应用于所有div(滚动前和滚动后)

谢谢。

解决方案一:

将你的click函数添加到全局作用域,如果内容改变了,重新分配:

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(更好):

检测父元素是否被单击,然后检查它是否是一个confirmdelete元素:

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

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