用JS切换/显示一个已定义的DIV [英] Toggle/Show one defined DIV with JS
本文介绍了用JS切换/显示一个已定义的DIV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我为您准备了一个小提琴: http://jsfiddle.net/vSs4f/
I have a fiddle for you: http://jsfiddle.net/vSs4f/
我想通过简单单击a.haschildren
来显示div.sub-menu
.如果主体负载div.sub-menu
,则应将其关闭.如果再次单击a.haschildren
,则div.sub-menu
应该关闭.
I want to show the div.sub-menu
with a simple click on a.haschildren
. If the body loads the div.sub-menu
should be closed. If I click a second time on a.haschildren
the div.sub-menu
should be close.
我已经采样了很多东西,但我认为问题在于DIV的很多.小提琴是一个主意.
I have sampled so many things but I think the problems are the lot of DIV's. One idea is in the fiddle.
$(function() {
$("a.haschildren").click(function(e) {
e.preventDefault();
$('div.sub-menu:visible').hide();
$(this).next('div.sub-menu').show();
});
});
我真的希望你能帮助我,谢谢!
I really hope you can help me, thanks!
推荐答案
尝试
$(function() {
$("a.haschildren").click(function(e) {
e.preventDefault();
var item = $(this).closest('div.haschildren').next().next('div.sub-menu').toggle();
$('div.sub-menu:visible').not(item).hide();
});
});
演示:小提琴
这篇关于用JS切换/显示一个已定义的DIV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文