문제

I'm trying to expand and collapse a <div> by clicking some text inside the <div>. The behavior right now is very odd. For example, if I click the text after the <div> is expanded... the <div> will collapse and then expand again. Also, if I click somewhere inside the div after it is expanded, it will collapse again, and I think that is because I'm triggering the animation since the <div> being animated is inside the wrapper <div>. Here's the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>

    <!-- Links -->
    <link rel="stylesheet" type="text/css" href="style.css" />

    <!-- Scripts -->
    <script type="text/javascript" src="jQuery.js"></script>
    <script>

        // document script
        $(function(){

            // login box event handler
            $('#login').click(function() {
                $('#loginBox').toggle(
                    function() {
                        $('.loginBox').animate({ 
                            height: '150px'
                        }, 
                            '1000'
                        );
                        $('#username').show();
                        $('#password').hide();
                        $('#placeHolder').show();                   
                    }, 
                    function() {
                        $('.loginBox').animate({ 
                            height: '50px'
                        }, 
                            '1000'
                        );
                        $('#username').hide();
                        $('#password').hide();
                        $('#placeHolder').hide();                                       
                    }
                );
            });


            // username field focus and blur event handlers
            $('#username').focus(function() {
                if($(this).hasClass('placeHolder')){
                    $(this).val('');
                    $(this).removeClass('placeHolder');
                }
            });
            $('#username').blur(function() {
                if($(this).val() == '') {
                    $(this).val('Username');
                    $(this).addClass('placeHolder');
                }
            });         

            // password field focus and blur event handlers
            $('#placeHolder').focus(function() {
                $(this).hide();
                $('#password').show();
                $('#password').focus();
                $('#password').removeClass('placeHolder');
            });
            $('#password').blur(function() {
                if($(this).val() == '') {
                    $('#placeHolder').show();
                    $(this).hide();
                }   
            });

        });

    </script>

</head>
<body>

    <div id="loginBox" class="loginBox">
        <a id="login" class="login">Proceed to Login</a><br />
        <div>
            <form>
                <input type="text" id="username" class="placeHolder" value="Username" />
                <input type="password" id="password" class="placeHolder" value="" />
                <input type="text" id="placeHolder" class="placeHolder" value="Password" />
            </form>
        </div>
    </div>

</body>
</html>

Any ideas?

도움이 되었습니까?

해결책

Yeah, use event.stopPropagation();

  $('#login').click(function(event) {
         event.stopPropagation();

that way, the event doesn't hit #loginBox. Also, if you're worried about animations getting stuck in the queue (clicking too many times and the animation going and going) you can use stop..

$('#username').stop().show();

further on toggle - your implemation adds a click handler to #loginBox on the click of login.. toggle is a click handler..

// login box event handler

 $('#login').toggle(
            function() {
                $('.loginBox').animate({ 
                    height: '150px'
                }, 
                    '1000'
                );
                $('#username').show();
                $('#password').hide();
                $('#placeHolder').show();                   
            }, 
            function() {
                $('.loginBox').animate({ 
                    height: '50px'
                }, 
                    '1000'
                );
                $('#username').hide();
                $('#password').hide();
                $('#placeHolder').hide();                                       
            }
        );

is a bit more accurate..

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top