[英]Hide and show when click a button
I have code like this (HTML/CSS/JS): 我有这样的代码(HTML / CSS / JS):
$('.subnav__right li a').click(function(e){ e.preventDefault(); var $this = $(this).parent().find('.subnav__hover'); $('.subnav__hover').not($this).hide(); $this.toggle(); }); $(document).mouseup(function (e) { var container = $('.subnav__hover'); if (!container.is(e.target) && container.has(e.target).length === 0) { container.hide(); } });
.subnav__hover{ display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="subnav__right"> <ul> <li class="subnav__notif"> <a href="#" class="np-ico-notification">Link 1</a> <div class="subnav__hover"> CONTENT 1</div> </li> <li class="subnav__caremail"> <a href="#" class="np-ico-caremail">Link 2</a> <div class="subnav__hover"> CONTENT 2</div> </li> <li class="subnav__profile"> <a href="#">Link 3</a> <div class="subnav__hover"> CONTENT 3</div> </li> </ul> </div>
The problem is toggle function (when I click .subnav__right li a
) doesn't work. 问题是切换功能(当我点击
.subnav__right li a
)不起作用。 But click the outside can hide .subnav__hover
. 但点击外面可以隐藏
.subnav__hover
。
Any idea how to make the toggle works? 知道如何使切换工作吗?
Use this: 用这个:
$(document).mouseup(function (e) { var container = $('.subnav__hover:visible'); var clickedObj = $(e.target).next('.subnav__hover'); if (!container.is(clickedObj) && container.has(e.target).length === 0) { container.hide(); } });
I have made small changes like change the document event and add e.stopPropagation()
. 我做了一些小改动,比如更改文档事件并添加
e.stopPropagation()
。 Please check. 请检查。
$('.subnav__right li a').click(function(e){ e.preventDefault(); e.stopPropagation(); var $this = $(this).parent().find('.subnav__hover'); $('.subnav__hover').not($this).hide(); $this.toggle(); }); $(document).click(function (e) { var container = $('.subnav__hover'); if (!container.is(e.target) && container.has(e.target).length === 0) { container.hide(); } });
.subnav__hover{ display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="subnav__right"> <ul> <li class="subnav__notif"> <a href="#" class="np-ico-notification">Link 1</a> <div class="subnav__hover"> CONTENT 1</div> </li> <li class="subnav__caremail"> <a href="#" class="np-ico-caremail">Link 2</a> <div class="subnav__hover"> CONTENT 2</div> </li> <li class="subnav__profile"> <a href="#">Link 3</a> <div class="subnav__hover"> CONTENT 3</div> </li> </ul> </div>
I think the click function should be: 我认为点击功能应该是:
$('.subnav__right ul li a').click(function(e){
e.preventDefault();
var $this = $(this).parent().find('.subnav__hover');
$('.subnav__hover').not($this).hide();
$this.toggle();
});
Bind this to the callback or use that. 将此绑定到回调或使用它。
var that = this;
$('.subnav__right li a').click(function(e){
e.preventDefault();
var $this = $(that).parent().find('.subnav__hover');
$('.subnav__hover').not($this).hide();
$this.toggle();
});
Maybe removing the not !
也许删除不
!
operator solves your problem (I guess), so your 2nd part of your JS code will be: 运算符解决了你的问题(我猜),所以你的JS代码的第二部分将是:
$(document).mouseup(function (e)
{
var container = $('.subnav__hover');
if (container.is(e.target) && container.has(e.target).length === 0)
{
container.hide();
}
});
http://jsfiddle.net/fsoh6h3z/1/ http://jsfiddle.net/fsoh6h3z/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.