简体   繁体   English

jQuery选择器-影响同一DOM元素内的元素

[英]Jquery selector - affecting element inside same DOM element

My CMS generates menus as lists without any id's and classes. 我的CMS将菜单生成为列表,没有任何ID和类。 For submenus, there are nested lists. 对于子菜单,有嵌套列表。

I made jquery script for expanding submenus: 我制作了用于扩展子菜单的jquery脚本:

$(function () {
    $(".wrapper ul li").click(function () {
        if ($(this).has("ul").length) {
            $("a", this).removeAttr('href');
            $("ul", this).slideToggle();
        }
    });
});

My problem is that this script reacts to clicking whole li area and I want it to react to clicking link inside li . 我的问题是此脚本对单击整个li区域有反应,我希望它对li内的单击链接作出反应。 Of course I just have to add "a" to selector making it ".wrapper ul li a" but what about condition checking if there is ul nested inside li ? 当然,我只需要添加的“A”到选择使它“.wrapper UL李一”但什么条件检查是否有ul嵌套在li And slidetoggle selector. 和slidetoggle选择器。 How should I change these? 我应该如何改变这些?

This might work with minimal modification to your original code. 这可能需要对原始代码进行最小的修改。

$(function () {
    $(".wrapper ul li a").click(function ()
        {
            this = $(this).parent();
            if ($(this).has("ul").length) {
                $("a", this).removeAttr('href');
                $("ul", this).slideToggle();
            }
        }
    );
});

Test and see if it works. 测试并查看是否有效。 I have not tested it. 我还没有测试。 Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM