jQuery mouseup和Undesired Behavior

时间:2015-09-16 15:50:49

标签: javascript jquery

我有一个"登录" div设计为按钮,单击该按钮将取消隐藏登录表单区域。

如果再次单击该按钮,则会隐藏登录表单区域。

然而,在一种情况下,它表现得不合需要。当表单显示(取消隐藏,点击登录按钮后),并且鼠标指针位于登录表单区域内,然后按下鼠标按钮(并按住它),然后将鼠标拖出登录表单区域,按下鼠标按钮,表单关闭。

我的问题是 - 如何在保留其他行为的同时防止这种不良行为?

HTML:

<div id="loginContainer">
<span id="loginButton"><span>log in</span></span>
<div id="register"><a href="#">sign up</a> or: </div>
<div id="loginBox">
    <form id="loginForm" method="post">
        <fieldset id="body">
            <input type="hidden" name="sAction" value="login" />
            <fieldset>                  
                <label for="sUserId">username or email</label>                  
                <input type="text" name="sUserId" id="sUserId" maxlength="255" />               
            </fieldset>             
            <fieldset>                  
                <label for="sUserPassword">password</label>                 
                <input type="password" name="sUserPassword" id="sUserPassword" maxlength="25" />                
            </fieldset>             
            <input type="submit" id="login" value="log in" />           
        </fieldset> 
    </form>
</div>
</div>

jQuery:

$(function () {
    var button = $('#loginButton');
    var box = $('#loginBox');
    var form = $('#loginForm');
    button.removeAttr('href');
    button.mouseup(function (login) {
        box.toggle();
        button.toggleClass('active');
    });
    form.mouseup(function () {
        return false;
    });
    $(this).mouseup(function (login) {
        if (!($(login.target).parent('#loginButton').length > 0)) {
            button.removeClass('active');
            box.hide();
        }
    });
});

CSS:

#register {padding:5px 10px 5px 0;float: right;}

/* Login Container */
#loginContainer {float:right;margin-top: 10px;}

/* Login, Logout Button */
#loginButton, #logout {display:inline-block;float:right;background:#99cc66;border-radius:3px;-moz-border-radius:3px;position:relative;z-index:30;cursor:pointer;}
#logout{color:#fff;height:32px;padding:5px 35px 5px 10px;background:#99cc66 url(../images/login-icon.png) no-repeat 62px 8px;display:block;border: 0;}

/* Login Button Text */
#loginButton span  {color:#fff;padding:5px 35px 5px 10px;background:url(../images/login-icon.png) no-repeat 58px 8px;display:block}

/* Login Box */
#loginBox {position:absolute;top:38px;right:0;display:none;z-index:29;}

/* If the Login Button has been clicked */    
#loginButton.active {border-radius:3px 3px 0 0;}

/* Login Form */
#loginForm {width:248px; border-radius:3px 0 3px 3px;-moz-border-radius:3px 0 3px 3px;margin-top:3px;background:#99cc66;padding:6px;}
#loginForm fieldset {margin:0 0 12px 0;display:block;border:0;padding:0;}
fieldset#body {background:#fff;border-radius:3px;-moz-border-radius:3px;padding:10px 13px;margin:0;}
#loginForm #rememberMe {width:auto;margin:1px 9px 0 0;float:left;padding:0;border:0;*margin:-3px 9px 0 0; /* IE7 Fix */}
#body label {color:#3a454d;margin:9px 0 0 0;display:block;float:left;}
#loginForm #body fieldset label {display:block;float:none;margin:0 0 6px 0;}

/* Default Input */
#loginForm input {width:92%;border:1px solid #899caa;border-radius:3px;-moz-border-radius:3px;color:#3a454d;padding:8px 8px;box-shadow:inset 0px 1px 3px #bbb;-webkit-box-shadow:inset 0px 1px 3px #bbb;-moz-box-shadow:inset 0px 1px 3px #bbb;font-size:12px;}

/* Sign In Button */
#loginForm #login {width:auto;float:right;background-color:#339cdf;color:#fff;padding:7px 10px 8px 10px;text-shadow:0px -1px #278db8;border:1px solid #339cdf;box-shadow:none;-moz-box-shadow:none;-webkit-box-shadow:none;margin:0 16px 0 0;cursor:pointer;*padding:7px 2px 8px 2px; /* IE7 Fix */}

/* Forgot your password */
#loginForm span {text-align:center;display:block;padding:7px 0 4px 0;}

#loginForm span a {color:#fff;}
input:focus {outline:none;}

1 个答案:

答案 0 :(得分:1)

如果您使用click而不是mouseup考虑了mousedown发生的位置),它会按预期工作。

$(function () {
    var button = $('#loginButton');
    var box = $('#loginBox');
    var form = $('#loginForm');
    button.removeAttr('href');
    button.click(function (login) {
        box.toggle();
        button.toggleClass('active');
    });
    form.click(function (e) {
        if (e.target.id !== 'login'){
          e.stopPropagation();
          }
    });
    $(this).click(function (login) {
        if (!($(login.target).parent('#loginButton').length > 0)) {
            button.removeClass('active');
            box.hide();
        }
    });
});
#register {
    padding:5px 10px 5px 0;
    float: right;
}
/* Login Container */
 #loginContainer {
    float:right;
    margin-top: 10px;
}
/* Login, Logout Button */
 #loginButton, #logout {
    display:inline-block;
    float:right;
    background:#99cc66;
    border-radius:3px;
    -moz-border-radius:3px;
    position:relative;
    z-index:30;
    cursor:pointer;
}
#logout {
    color:#fff;
    height:32px;
    padding:5px 35px 5px 10px;
    background:#99cc66 url(../images/login-icon.png) no-repeat 62px 8px;
    display:block;
    border: 0;
}
/* Login Button Text */
 #loginButton span {
    color:#fff;
    padding:5px 35px 5px 10px;
    background:url(../images/login-icon.png) no-repeat 58px 8px;
    display:block
}
/* Login Box */
 #loginBox {
    position:absolute;
    top:38px;
    right:0;
    display:none;
    z-index:29;
}
/* If the Login Button has been clicked */
 #loginButton.active {
    border-radius:3px 3px 0 0;
}
/* Login Form */
 #loginForm {
    width:248px;
    border-radius:3px 0 3px 3px;
    -moz-border-radius:3px 0 3px 3px;
    margin-top:3px;
    background:#99cc66;
    padding:6px;
}
#loginForm fieldset {
    margin:0 0 12px 0;
    display:block;
    border:0;
    padding:0;
}
fieldset#body {
    background:#fff;
    border-radius:3px;
    -moz-border-radius:3px;
    padding:10px 13px;
    margin:0;
}
#loginForm #rememberMe {
    width:auto;
    margin:1px 9px 0 0;
    float:left;
    padding:0;
    border:0;
    *margin:-3px 9px 0 0;
    /* IE7 Fix */
}
#body label {
    color:#3a454d;
    margin:9px 0 0 0;
    display:block;
    float:left;
}
#loginForm #body fieldset label {
    display:block;
    float:none;
    margin:0 0 6px 0;
}
/* Default Input */
 #loginForm input {
    width:92%;
    border:1px solid #899caa;
    border-radius:3px;
    -moz-border-radius:3px;
    color:#3a454d;
    padding:8px 8px;
    box-shadow:inset 0px 1px 3px #bbb;
    -webkit-box-shadow:inset 0px 1px 3px #bbb;
    -moz-box-shadow:inset 0px 1px 3px #bbb;
    font-size:12px;
}
/* Sign In Button */
 #loginForm #login {
    width:auto;
    float:right;
    background-color:#339cdf;
    color:#fff;
    padding:7px 10px 8px 10px;
    text-shadow:0px -1px #278db8;
    border:1px solid #339cdf;
    box-shadow:none;
    -moz-box-shadow:none;
    -webkit-box-shadow:none;
    margin:0 16px 0 0;
    cursor:pointer;
    *padding:7px 2px 8px 2px;
    /* IE7 Fix */
}
/* Forgot your password */
 #loginForm span {
    text-align:center;
    display:block;
    padding:7px 0 4px 0;
}
#loginForm span a {
    color:#fff;
}
input:focus {
    outline:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="loginContainer">
    <span id="loginButton"><span>log in</span></span>
    <div id="register"><a href="#">sign up</a> or:</div>
    <div id="loginBox">
        <form action="test" id="loginForm" method="post">
            <fieldset id="body">
                <input type="hidden" name="sAction" value="login" />
                <fieldset>
                    <label for="sUserId">username or email</label>
                    <input type="text" name="sUserId" id="sUserId" maxlength="255" />
                </fieldset>
                <fieldset>
                    <label for="sUserPassword">password</label>
                    <input type="password" name="sUserPassword" id="sUserPassword" maxlength="25" />
                </fieldset>
                <input type="submit" id="login" value="log in" />
            </fieldset>
        </form>
    </div>
</div>

评论后更新

在表单的false事件上返回click会导致其无法提交。

所以我们需要将其改为

form.click(function (e) {
    if (e.target.id !== 'login'){
      e.stopPropagation();
      }
});