我希望登录区域(在下面的代码中)切换登录按钮单击。另外,我需要在登录区域以外的任何地方点击登录区域,而不是登录按钮。点击外部和更多
<button id="loginBtn">Login</button>
<div id="loginArea">
<!-- here is login form -->
</div>
我该如何做到这一点?
我希望登录区域(在下面的代码中)切换登录按钮单击。另外,我需要在登录区域以外的任何地方点击登录区域,而不是登录按钮。点击外部和更多
<button id="loginBtn">Login</button>
<div id="loginArea">
<!-- here is login form -->
</div>
我该如何做到这一点?
$(function() {
$('#loginBtn, #loginArea').on('click', function(e) {
$('#loginArea').show();
e.stopPropagation();
});
$('body').on('click', function() {
$('#loginArea').hide();
});
});
如果点击位于登录区域内或按钮上,则该事件将不会使其进入第二个处理程序以隐藏登录区域。
使用:
$('body').on('click', '#loginBtn', function(){
$('#loginArea').show();
});
会显示登录,而下面将删除它,当你点击它外面:
$(document).on('click', 'body:not(#loginArea)', function(){
$('#loginArea').hide();
});
.remove()将从DOM中删除div。 – Dogoku 2013-03-12 13:32:50
@Dogoku啊好抓!更新。 – 2013-03-12 13:33:32
是如何形成的?
$('#loginBtn').click(function (e) {
$('#loginArea').toggle();
e.stopPropagation();
});
$(document).on('click',function() {
$('#loginArea').hide();
});
即使单击它,也会隐藏登录框。 – 2013-03-12 13:32:12
不,它不会,'e.stopPropagation()'停止事件冒泡到其他处理程序 – Dogoku 2013-03-12 13:33:46
@eomeroff:你有1151代表 - 当然,你现在应该知道,人们会问你 - “你尝试过什么到目前为止,” ......关于使用选择如何 – Paul 2013-03-12 13:29:46
:不是? – egig 2013-03-12 13:30:28
@Westie我没有1151了。而且我没有看到有人问我试过了什么。如果我不知道如何尝试,该怎么办?这是一个无法研究的具体问题。老实说,我没有看到5倒票的原因。希望这个问题对某个人有用。 – eomeroff 2013-03-12 13:42:53