Php 切换HTML元素的代码被调用两次
这是剧本Php 切换HTML元素的代码被调用两次,php,jquery,html,Php,Jquery,Html,这是剧本 // Here based on the class rply1 I am displaying replies <div class="biz_gary_btn width80 pl5 fl rply1<?php echo $comm['comment_id'];?>"> <div class="fl pt5_ie pt1"><img src="<?php echo base_url();?>images/green_ca
// Here based on the class rply1 I am displaying replies
<div class="biz_gary_btn width80 pl5 fl rply1<?php echo $comm['comment_id'];?>">
<div class="fl pt5_ie pt1"><img src="<?php echo base_url();?>images/green_callot.png" alt="Green Callot"></div>
<div class="arial bold fnt11 c7b7b7b fl pl5 ">2 Replies </div>
</div>
<!--2 Replies-->
<!--Reply-->
<div class="biz_gary_btn width84 pl5 fl rply2<?php echo $comm['comment_id'];?>">
<div class="fl pt5_ie pt1 pl5"><img src="<?php echo base_url();?>images/light_blue_callot.png" alt="Blue Callot"></div>
<div class="arial bold fnt11 c7b7b7b fl pl7">Reply</div>
</div>
但是函数被调用了两次。我可以看到警报两次。第一次警报后,我的2replies div可见。这在HTML设置中工作得很好。这可能不太理想,因为如果没有看到完整的代码,很难判断是否实际绑定了两次事件。但是,您可以在再次绑定click事件之前确保该事件已解除绑定:
<script type="text/javascript">
$(document).ready(function(){
$(".rply1<?php echo $comm['comment_id'];?>").click(function(){
alert("sdfsd");
//return true;
$("#2replies<?php echo $comm['comment_id'];?>").toggle();
$(".rply1<?php echo $comm['comment_id'];?>").toggleClass("advice_white_btn_active");
});
$(".rply2<?php echo $comm['comment_id'];?>").click(function(){
$("#reply<?php echo $comm['comment_id'];?>").toggle();
$(".rply2<?php echo $comm['comment_id'];?>").toggleClass("advice_white_btn_active");
});
});
</script>
我尝试在脚本无效后执行return false和return true。它会被调用两次。如果你从某个地方包含此代码,你必须包含两次。这是一个解决方法,但我不认为它是一个解决方案,如果你从其他地方附加其他侦听器,你将被排除在爱之外。。。
<script type="text/javascript">
$(document).ready(function(){
$(".rply1<?php echo $comm['comment_id'];?>").unbind("click").bind("click", function(){
alert("sdfsd");
//return true;
$("#2replies<?php echo $comm['comment_id'];?>").toggle();
$(".rply1<?php echo $comm['comment_id'];?>").toggleClass("advice_white_btn_active");
});
$(".rply2<?php echo $comm['comment_id'];?>").unbind("click").bind("click", function(){
$("#reply<?php echo $comm['comment_id'];?>").toggle();
$(".rply2<?php echo $comm['comment_id'];?>").toggleClass("advice_white_btn_active");
});
});
</script>