简体   繁体   English

具有事件处理程序的Javascript变量作用域行为异常

[英]Javascript variable scope with event handler acts weird

<script>  
            var rotateFlag = 0;
            $(document).ready(function(){
                console.log(rotateFlag);
                dropdown();
                getdown();
            });
            function dropdown(){
                if(rotateFlag===0){
                    $('.comment-box').on('click',function(event){
                        console.log("when rotateFlag is 0", rotateFlag);
                        $(this).css('transform','rotate(-90deg)');
                        rotateFlag = 1; 
                    });
                }
                if(rotateFlag===1){
                    $('.comment-box').on('click',function(event){
                        console.log("when rotateFlag is 1", rotateFlag);
                        $('.comment-box').css('transform','rotate(0deg)');
                        rotateFlag = 0;
                    })
                }
            }
            function getdown(){ 
                $(document).on('click',function(event){

                    if( $(event.target)[0].className !=  'comment-box' && 
                        $(event.target)[0].className != 'dot' ){
                        console.log('getdown');
                        $('.comment-box').css('transform', 'rotate(0deg)'); 
                        rotateFlag = 0;
                    }
                })
            }
    <script>  
                <div class="comment-box"> 
                    <span class="dot"></span>
                    <span class="dot"></span>
                    <span class="dot"></span>
                </div>

I tried to transform element div named comment-box , and I faced one problem that variable rotateFlag printed 1 even in if(rotateFlag ===0) . 我试图转换名为comment-box div元素,并且遇到了一个问题,即使在if(rotateFlag ===0) ,变量rotateFlag 1。 It's weird.. look at the code below: 很奇怪..看下面的代码:

            var rotateFlag = 0;
            $(document).ready(function(){
                console.log(rotateFlag);
                dropdown();
                getdown();
            });
            function dropdown(){
                if(rotateFlag===0){
                    $('.comment-box').on('click',function(event){
                        console.log("when rotateFlag is 0", rotateFlag);
                        $(this).css('transform','rotate(-90deg)');
                        rotateFlag = 1; 
                    });
                }

I assigned variable rotateFlag , and let button event handles only when rotateFlag has value 1. The first time I click 'comment-box' it prints when rotateFlag is 0 0 however, from twice, it returns like this. 我分配了变量rotateFlag ,让按钮事件仅在rotateFlag值为1时处理。第一次单击“注释框”时,它将在rotateFlag为0 0时打印,但是从两次开始,它会像这样返回。 when rotateFlag is 0 1 当rotateFlag为0 1时

weird .... can't get Ideas why this happens. 很奇怪..无法得到想法为什么会这样。

It's not weird.. When you call dropdown function(at the first time), rotateFlag is 0. 这并不奇怪。当您调用下拉函数(第一次)时,rotateFlag为0。

if(rotateFlag===0){
                $('.comment-box').on('click',function(event){
                    console.log("when rotateFlag is 0", rotateFlag);
                    $(this).css('transform','rotate(-90deg)');
                    rotateFlag = 1; 
                });
            }

So above click event is applied always. 因此,以上点击事件始终适用。

If you want to apply another click event, off the click event and call dropdown function when getdown function called. 如果要应用另一个click事件,请在调用getdown函数时关闭click事件并调用dropdown函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM