简体   繁体   English

jQuery类型的第一个元素单击

[英]jQuery first element of type click

I am working through an issue where I have a menu that is mutli levels deep. 我正在解决一个问题,其中有一个菜单级别很深。 I am trying to get it so that if a first level element is clicked (add class .ubermenu-active ) it will look for any other first level element with that class and remove it. 我正在尝试获取它,以便如果单击第一级元素(添加类.ubermenu-active ),它将寻找该类的任何其他第一级元素并将其删除。

$('.ubermenu-item-level-0 span').on('click',function() {
    var target = $('.ubermenu-item-level-0');
    $('.ubermenu li.ubermenu-item-level-0').removeClass('ubermenu-active');
    target.parents('.ubermenu li.ubermenu-item-level-0').toggleClass('ubermenu-active');
});

HTML (mock): HTML(模拟):

<ul class="ubermenu">
    <li class="ubermenu-item-level-0">
        <a class="ubermenu-target">
            <span class="ubermenu-target-title">Level 1</span>
        </a>
        <ul class="ubermenu-submenu">
            <li class="ubermenu-item-level-1">
                <a class="ubermenu-target">
                    <span class="ubermenu-target-title">Level 2</span>
                </a>
            </li>
            <li class="ubermenu-item-level-1">
                <a class="ubermenu-target">
                    <span class="ubermenu-target-title">Level 2</span>
                </a>
                <ul class="ubermenu-submenu">
                    <li class="ubermenu-item-level-2">
                        <a class="ubermenu-target">
                            <span class="ubermenu-target-title">Level 3</span>
                        </a>
                    </li>
                </ul>
            </li>
        </ul>
        <li class="ubermenu-item-level-0">
            <a class="ubermenu-target">
                <span class="ubermenu-target-title">Level 1</span>
            </a>
        </li>
    </li>
</ul>

Right now if any of the sub elements are clicked the parent closes 现在,如果单击任何子元素,父级将关闭

Currently your target var is selecting all elements with class .ubermenu-item-level-0 , so when you toggle the class you're toggling all parent elements. 当前,您的target var正在选择类.ubermenu-item-level-0所有元素,因此,当您切换类时,您将切换所有父元素。 Your target var should be something relative to the element clicked, like var target = $(this).closest('.ubermenu-item-level-0'); 您的目标var应该是与所单击元素有关的东西,例如var target = $(this).closest('.ubermenu-item-level-0');

您可以停止事件传播:

event.stopPropagation()

Without seeing the full HTML, I was going to suggest trying this: 在没有看到完整的HTML的情况下,我建议尝试这样做:

$('.ubermenu-item-level-0 span').on('click',function(e) {
       e.stopPropagation();
       var target = $('.ubermenu-item-level-0');
       $('.ubermenu li.ubermenu-item-level-0').removeClass('ubermenu-active');
       target.parents('.ubermenu li.ubermenu-item-level-0').toggleClass('ubermenu-active');
    });

So, your sample HTML isn't what I expected to see . 因此,您的示例HTML不是我期望看到的。 . . specifically, your second <li class="ubermenu-item-level-0" . . . 具体来说,您的第二个<li class="ubermenu-item-level-0" . . . <li class="ubermenu-item-level-0" . . . element is currently showing as a child under the first one, while your description makes it sound like they should be siblings. 元素当前在第一个元素下显示为儿童,而您的描述听起来像他们应该是兄弟姐妹。

For the sake of the solution I'm going to assume that those two li s are supposed to be siblings of each other and that somehow the code got mixed up. 为了解决这个问题,我将假定这两个li是彼此的兄弟,并且代码以某种方式混淆了。 :) :)

So, here's how I would handle it . 所以,这就是我的处理方式。 . .

var sFirstLevelMenuClass = ".ubermenu-item-level-0";
var sActiveMenuClass = "ubermenu-active";
var $firstLevelMenuOptions = $(".ubermenu").find(sFirstLevelMenuClass);

$firstLevelMenuOptions.children("a").children("span").on("click", function() {
    $firstLevelMenuOptions.removeClass(sActiveMenuClass);
    $(this).closest(sFirstLevelMenuClass).addClass(sActiveMenuClass);
});

Basically, I've simplified your logic and fixed one small issue that you had in your jQuery code. 基本上,我简化了您的逻辑并修复了jQuery代码中的一个小问题。


Detailed Explanation 详细说明

The issue was that when you used $('.ubermenu-item-level-0 span') as your selector for your change event. 问题是,当您使用$('.ubermenu-item-level-0 span')作为更改事件的选择器时。 That translates to "any span element that is a descendant of a ubermenu-item-level-0 element". 那就是“任何作为ubermenu-item-level-0元素的后代的span元素”。 So, in addition to the spans that are directly under the ubermenu-item-level-0 list items, it was also picking up the ones under the ubermenu-item-level-1 and ubermenu-item-level-2 elements, since they are also descendants. 因此,除了直接在ubermenu-item-level-0列表项下的跨度之外,它还选择了ubermenu-item-level-1ubermenu-item-level-2元素下的ubermenu-item-level-1 ,因为它们也是后代。

So, I changed your selector to $firstLevelMenuOptions.children("a").children("span") which translates to "all span s, that are direct children of an a , that is the direct child of an ubermenu-item-level-0 element" ( Note: $firstLevelMenuOptions is set to equal $(".ubermenu").find(".ubermenu-active"); through the logic earlier in the code ). 因此,我将选择器更改为$firstLevelMenuOptions.children("a").children("span") ,它转换为“所有span ,即a的直接子代,即ubermenu-item-level-0的直接ubermenu-item-level-0 element”( 注意: $firstLevelMenuOptions设置为等于$(".ubermenu").find(".ubermenu-active");通过代码前面的逻辑 )。 This stops the selector from picking up the lower level spans . 这将阻止选择器拾取较低的水平spans

Outside of that and trimming down some of your selectors to be more efficient, the only other thing that I changed was the flow of how the ubermenu-active class is manipulated. 除此之外,还精简了一些选择器以提高效率,我唯一改变的另一件事是如何操作ubermenu-active类。 In my solution, there are two steps: 在我的解决方案中,有两个步骤:

  1. Remove ubermenu-active from ALL ubermenu-item-level-0 elements 所有 ubermenu-item-level-0元素中删除ubermenu-active
  2. Add ubermenu-active to the closest ubermenu-item-level-0 element to the span that was clicked (ie, $(this) ) ubermenu-active添加到与所单击span最接近的ubermenu-item-level-0元素(即$(this) )。

That basically resets the list and then reselects the appropriate menu item to me active. 这基本上是重置列表,然后重新选择适当的菜单项激活我。

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

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