[英]Is it possible to hide two divs from two different .php pages with a unique javascript function?
[英]Applying a javascript code for two different php pages
在我的项目中,我有这个代码,它从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,但在向下滚动页面之前,它仅适用于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;
});
我如何对所有DIV应用此javascrip代码(滚动之前和滚动之后)
谢谢。
解决方案1:
将您的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/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.