jquery - 无法关闭我的覆盖登录表单

标签 jquery html css

我正在使用 jquery 创建一个叠加层,当用户单击导航中的“登录”列表项时,该叠加层就会显示出来。 Fiddle

到目前为止一切正常。唯一的问题是试图让它关闭。我无法让覆盖框消失。

当我查看控制台时,我得到 - “未捕获的类型错误:未定义不是函数”第 111 行

第 111 行是我的关闭函数开始的地方。我不确定我做错了什么。这是第 111 行的代码

    $('a.close, #mask').live('click', function () {
        $('#mask , .login-popup').fadeOut(300, function () {
            $('#mask').remove();
        });
        return false;
    });

这是完整的 Jquery

<script type="text/javascript">
$(document).ready(function () {
    $('a.login-window').click(function () {

        // Getting the variable's value from a link
        var loginBox = $(this).attr('href');

        //Fade in the Popup and add close button
        $(loginBox).fadeIn(300);

        //Set the center alignment padding + border
        var popMargTop = ($(loginBox).height() + 24) / 2;
        var popMargLeft = ($(loginBox).width() + 24) / 2;

        $(loginBox).css({
            'margin-top': -popMargTop,
            'margin-left': -popMargLeft
        });

        // Add the mask to body
        $('body').append('<div id="mask"></div>');
        $('#mask').fadeIn(300);

        return false;
    });

    // When clicking on the button close or the mask layer the popup closed
    $('a.close, #mask').live('click', function () {
        $('#mask , .login-popup').fadeOut(300, function () {
            $('#mask').remove();
        });
        return false;
    });
});
</script>

HTML

 <div id="login-box" class="login-popup">
        <a href="#" class="close"><img src="images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>
        <form action="https://www.theiracenter.com/customerLoginAction.gsx" method="post">
            <label for="username">Username:</label>
            <input class="topLogin" type="text" name="login" width="125px" />
            <br />
            <label for="password">Password:</label>
            <input class="topLogin" id="passwordText" type="password" name="password" width="125px" />
            <input type="hidden" name="pd" value="C05" />
            <br />
            <input type="submit" id="loginSubmit" value="Submit" />
        </form>
    </div>

还有 CSS

/* Mask for background, by default is not display */
#mask {
    display: none;
    background: #000;
    position: fixed;
    left: 0;
    top: 0;
    z-index: 10;
    width: 100%;
    height: 100%;
    opacity: 0.8;
    z-index: 999;
}

/* You can customize to your needs  */
.login-popup {
    display: none;
    background: #333;
    padding: 10px;
    border: 2px solid #ddd;
    float: left;
    font-size: 1.2em;
    position: fixed;
    top: 50%;
    left: 50%;
    z-index: 99999;
    box-shadow: 0px 0px 20px #999;
    /* CSS3 */
        -moz-box-shadow: 0px 0px 20px #999;
    /* Firefox */
        -webkit-box-shadow: 0px 0px 20px #999;
    /* Safari, Chrome */
    border-radius: 3px 3px 3px 3px;
    -moz-border-radius: 3px;
    /* Firefox */
        -webkit-border-radius: 3px;
    /* Safari, Chrome */;
}

img.btn_close {
    Position the close button
    float: right;
    margin: -28px -28px 0 0;
}

fieldset {
    border: none;
}

form.signin .textbox label {
    display: block;
    padding-bottom: 7px;
}

form.signin .textbox span {
    display: block;
}

form.signin p, form.signin span {
    color: #999;
    font-size: 11px;
    line-height: 18px;
}

form.signin .textbox input {
    background: #666666;
    border-bottom: 1px solid #333;
    border-left: 1px solid #000;
    border-right: 1px solid #333;
    border-top: 1px solid #000;
    color: #fff;
    border-radius: 3px 3px 3px 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    font: 13px Arial, Helvetica, sans-serif;
    padding: 6px 6px 4px;
    width: 200px;
}

form.signin input:-moz-placeholder {
    color: #bbb;
    text-shadow: 0 0 2px #000;
}

form.signin input::-webkit-input-placeholder {
    color: #bbb;
    text-shadow: 0 0 2px #000;
}

.button {
    background: -moz-linear-gradient(center top, #f3f3f3, #dddddd);
    background: -webkit-gradient(linear, left top, left bottom, from(#f3f3f3), to(#dddddd));
    background: -o-linear-gradient(top, #f3f3f3, #dddddd);
    filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#f3f3f3', EndColorStr='#dddddd');
    border-color: #000;
    border-width: 1px;
    border-radius: 4px 4px 4px 4px;
    -moz-border-radius: 4px;
    -webkit-border-radius: 4px;
    color: #333;
    cursor: pointer;
    display: inline-block;
    padding: 6px 6px 4px;
    margin-top: 10px;
    font: 12px;
    width: 214px;
}

.button:hover {
    background: #ddd;
}

最佳答案

.live()自 jQuery 1.7 起已弃用。并且您使用的是 1.8+!您应该使用 .on()

$(document.body).on('click','a.close, #mask', function () {
      $('#mask , .login-popup').fadeOut(300, function () {
          $('#mask').remove();
      });
     return false;
});

Fiddle

关于jquery - 无法关闭我的覆盖登录表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23829687/

相关文章:

php - 在输入类型文件浏览窗口中只显示图像文件?

javascript - 点击事件在 Firefox 中不起作用

javascript - Jquery:TypeError: $(...)._TMS 不是函数

css - 位置 :absolute image inside position:relative not showing in IE

javascript - jQuery 隐藏不同表上的列

javascript - 设置附加到文本区域的字符串的顺序

html - 在 IE 中无法点击的链接

html -::first-letter 伪元素的首字母属性不起作用

javascript - jQuery ReplaceWith 在 Firefox 42 中不起作用

javascript - Sidr.js 菜单在桌面上禁用