我想在单击 email-signup-link
时打开 email-signup
。然后,我想通过单击页面上除框本身以外的任意位置来关闭它。
我在这个网站上四处看了看,有多种解决方案可以解决这个问题,但我尝试过的每一个都会立即显示并隐藏我的 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();
});
关于javascript - jQuery 通过单击页面上的任意位置关闭 DIV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9438583/