开发者

Code to toggle HTML-elements is called twice

// 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 Rep开发者_如何学Clies-->
<!--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>

This is the script

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

But the function is being called twice. I can see the alert twice. After the first alert my #2replies div is visible. This working just fine in the HTML set-up.


This might not be ideal because without seeing your full code it is hard to tell if you are actually binding the event twice. But, instead you could ensure that a click event is unbound before binding it again:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜