javascript - 单击时显示父 LI 的 UL

标签 javascript jquery

我正在尝试找出我需要为以下函数编写的 jQuery。当有人单击跨度“展开”时,我希望能够显示包含单击的跨度的 LI 的子菜单 (UL)。

默认情况下,子 UL 通过 CSS 隐藏。但是当单击展开时,我想显示它。

例如,请参阅下面的代码...

<li id="menu-item-15031" class="menu-item menu-item-type-post_type menu-item-object-page current-page-ancestor current-menu-ancestor current-page-parent current_page_ancestor menu-item-15031"><a href="http://www.tintworld.com/automotive-services/">Automotive Services</a>
<ul class="sub-menu">
    <li id="menu-item-15039" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-ancestor current-menu-parent current_page_parent current_page_ancestor menu-item-15039"><a href="http://www.tintworld.com/automotive-services/window-tinting/">Window Tinting</a>
        <ul class="sub-menu">
            <li id="menu-item-15033" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-484 current_page_item menu-item-15033"><a href="http://www.tintworld.com/automotive-services/car-window-tinting/">Car Window Tinting</a></li>
            <li id="menu-item-15040" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-15040"><a href="http://www.tintworld.com/automotive-services/computer-cut-film-system/">Computer Cut Window Tint System</a></li>
            <li id="menu-item-15041" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-15041"><a href="http://www.tintworld.com/automotive-services/solar-window-tint/">Solar Window Tint</a></li>
            <li id="menu-item-15042" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-15042"><a href="http://www.tintworld.com/automotive-services/custom-tint-designs/">Custom Tint Designs</a></li>
            <li id="menu-item-15043" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-15043"><a href="http://www.tintworld.com/automotive-services/removing-window-tint/">Removing Window Tint</a></li>
            <li id="menu-item-15044" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-15044"><a href="http://www.tintworld.com/automotive-services/window-tint-regulations/">Window Tint Regulations</a></li>
        </ul>
    </li>
</ul>
<span class="arrow"></span>
</li>

我知道 jQuery 非常简单,但说实话,我已经筋疲力尽,现在无法全神贯注。

请帮助我。 :)

最佳答案

尝试其中任何一个。请让你疲惫的心灵休息:)

$(".arrow").click( function () {
 $(this).closest("li").find("ul.sub-menu").slideToggle("slow");
});

或者

 $(".arrow").click( function () {
 $(this).prev("ul.sub-menu").slideToggle("slow");
 });

或者

 $(".arrow").click( function () {
 $(this).siblings("ul.sub-menu").slideToggle("slow");
 });

关于javascript - 单击时显示父 LI 的 UL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16828173/

相关文章:

asp.net - 禁用 Google 工具栏自动填充

javascript - data-* 在点击后不更新

javascript - Jquery获取列表元素的id并根据点击调用函数

javascript - 适用于 Chrome 但不适用于 Internet Explorer

javascript - 无法在 jQuery 选择器中使用变量

javascript - 检查并添加数组对象中的属性

javascript - 尝试运行平均堆栈时未处理的 'error' 事件

javascript - 动态循环 JSON 并在设定的时间间隔后显示和隐藏对象元素

javascript - 使用jQuery根据背景颜色查找div

jquery - 是否有任何 jquery 插件/脚本可以检测用户系统上的 Adob​​e Reader?