jQuery 嵌套 ul 与 li 的父子,显示隐藏
我正在尝试创建一个简单的手风琴菜单,其中嵌套在 li 中的 ul 被隐藏,直到单击父 li,这将首先隐藏所有其他嵌套的 ul(如果暴露),然后暴露该 li 的嵌套 ul。
唯一的另一个问题是我想删除父 li 的 href。
我已经实现了大部分目标,除了我的嵌套 ul li 继承了上述操作,即使我的目标是 li 的父类。我知道我错过了一些基本的东西。提前感谢
HTML
<ul>
<li.expanded><a href="foo">Lorum</a>
<ul>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
</ul>
</li>
<li.expanded><a href="foo">Lorum</a>
<ul>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
</ul>
</li>
<li.expanded><a href="foo">Lorum</a>
<ul>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
</ul>
</li>
<li.expanded><a href="foo">Lorum</a>
<ul>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
</ul>
</li>
</ul>
jQuery
$(document).ready(function() {
$('ul ul').hide();
$('ul li.expanded a').removeAttr("href");
$('ul li').click(function(){
$('ul ul').hide('slow');
$(this).find('ul').toggle('slow');
});
});
;
I'm trying to create a simple accordion menu where ul's nested in an li are hidden until the parent li is clicked which will first hide all other nested ul's if exposed and then expose the nested ul of that li.
The only other catch is that I want to remove the href for the parent li.
I've achieved most of this except my nested ul li's are inheriting the aforementioned actions even though I'm targeting the li's parent class. I know I'm missing something fundamental. Thanks in advance
HTML
<ul>
<li.expanded><a href="foo">Lorum</a>
<ul>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
</ul>
</li>
<li.expanded><a href="foo">Lorum</a>
<ul>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
</ul>
</li>
<li.expanded><a href="foo">Lorum</a>
<ul>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
</ul>
</li>
<li.expanded><a href="foo">Lorum</a>
<ul>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
<li.leaf><a href="foo">Lorum</a></li>
</ul>
</li>
</ul>
jQuery
$(document).ready(function() {
$('ul ul').hide();
$('ul li.expanded a').removeAttr("href");
$('ul li').click(function(){
$('ul ul').hide('slow');
$(this).find('ul').toggle('slow');
});
});
;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
html 中有一些拼写错误,我已将 li.leaf 更改为 class='leaf'。我对剧本也有改动。使用 css 来隐藏所有子 ul。
查看实际效果:http://jsfiddle.net/theuideveloper/DFHWd/4/
希望这对您有帮助。
快乐编码!
-theUiDeveloper
HTML
css
jQuery
There were some typos in the html, I have the changed li.leaf to class='leaf'. I have changes in the script too. To hide all the sub ul's css is used.
See it in action : http://jsfiddle.net/theuideveloper/DFHWd/4/
Hope this helps you.
Happy coding!
-theUiDeveloper
HTML
css
jQuery
您可以按如下方式更改 jQuery:
问题是目标
li
的子元素是 冒泡其click
事件,触发li.expanded
的点击事件处理程序。我已更改选择器,以直接将a
定位为li
类expanded
下的目标。这是一个工作示例: http://jsfiddle.net/andrewwhitaker/ybVFj/
另外,您的 HTML 应该看起来像这样:
You could change your jQuery as follows:
The problem was that child elements of the targeted
li
were bubbling theirclick
event up, triggeringli.expanded
's click event handler. I've changed the selector to target thea
directly under anli
with classexpanded
.Here's a working example: http://jsfiddle.net/andrewwhitaker/ybVFj/
Also, your HTML should look something like this: