jQuery 通过单击页面上的任意位置关闭 DIV

2023-11-30

我想打开email-signup当我点击email-signup-link。当然,然后我想通过单击页面上除框本身之外的任何位置来关闭它。

我浏览过这个网站,有多种解决方案可以解决这个问题,但我尝试过的每一个解决方案都会显示然后立即隐藏我的 div。我一定做错了什么。

这是 HTML:

<a href="#" id="email-signup-link">Sign Up</a>
<div id="email-signup">
    <div id="inner">
        <h2>E-mail Notifications</h2>
        <input class="" type="text" name="description" placeholder="Enter your e-mail address" id="description" />
        <a href="#">Sign Up</a>
    </div>
</div>

这是我的 JavaScript:

$('#email-signup').click(function(){
    e.stopPropagation();
});
$("#email-signup-link").click(function() {
    e.preventDefault();
    $('#email-signup').show();
});
$(document).click(function() {
    $('#email-signup').hide();
});

两件事情。你实际上没有e已定义,因此您无法使用它。而你需要stopPropagation在您的其他点击处理程序中:

$('#email-signup').click(function(e){
    e.stopPropagation();
});
$("#email-signup-link").click(function(e) {
    e.preventDefault();
    e.stopPropagation();
    $('#email-signup').show();
});
$(document).click(function() {
    $('#email-signup').hide();
});​

http://jsfiddle.net/Nczpb/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jQuery 通过单击页面上的任意位置关闭 DIV 的相关文章

随机推荐