在我的项目中,我有这段代码,它从 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>
我想在我的评论 DIV 中应用下面的 JavaScript,但它仅适用于向下滚动页面之前的 DIVS
$('#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(滚动之前和滚动之后)
谢谢。
最佳答案
解决方案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
});
关于javascript - 将 javascript 代码应用于两个不同的 php 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40493414/