简体   繁体   English

如何只选择具有相同类名的元素列表中的第一个元素

[英]How select only the first element of a list of elements with the same class name

I have the following code in jquery to display items for a sub menu: 我在jquery中有以下代码来显示子菜单的项目:

$(document).ready(function(){
  $(".menu > .menu-item").mouseenter(function(){
    if($(this).has(".sub-menu").length){
      $(".sub-menu",this).fadeIn(500);
      $(".sub-menu",this).css("margin","0 40px");
      $(".sub-menu",this).css("position","fixed");
      $(".sub-menu",this).css("z-index","10");
    }
  })
  .mouseleave(function(){
    if($(this).has(".sub-menu").length){
      $(".sub-menu",this).fadeOut(500);
    }
  });
});

It works fine when the sub menu element doesn't have other sub elements, but when I add a sub element both are displayed at the same time. 当子菜单元素没有其他子元素时,它工作正常,但是当我添加一个子元素时,两个元素同时显示。

I want to display only the sub elements and if any sub element has more sub elements inside of it, display them as well only when the mouse is over the main sub element. 我只想显示子元素,并且如果任何子元素中有更多子元素,则仅当鼠标悬停在主子元素上时才显示它们。

This is the code generated automatically when I add sub elements to the menu: 这是将子元素添加到菜单时自动生成的代码:

<li id="menu-item-12" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-12">
  <a href="http://localhost:8080/testoir/sample-page/">Audiologia</a>
  <ul class="sub-menu" style="display: none; margin: 0px 40px; position: fixed; z-index: 10;">
    <li id="menu-item-17" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-17"><a href="#">option1</a>
      <ul class="sub-menu" style="display: none; margin: 0px 40px; position: fixed; z-index: 10;">
        <li id="menu-item-18" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-18"><a href="#">option2</a></li>
      </ul>
    </li>
  </ul>
</li>

How can I do this? 我怎样才能做到这一点?

Thank you very much for your time and help. 非常感谢您的时间和帮助。

$(".sub-menu", this) will match sub-menus at any depth. $(".sub-menu", this)将在任何深度匹配子菜单。 If you just want the immediate children, use $(this).children(".sub-menu") . 如果您只想要$(this).children(".sub-menu")子,请使用$(this).children(".sub-menu")

 $(document).ready(function() { $(".menu > .menu-item").mouseenter(function() { if ($(this).children(".sub-menu").length) { $(this).children(".sub-menu") .fadeIn(500) .css({ "margin": "0 40px", "position": "fixed", "z-index": "10" }); } }) .mouseleave(function() { if ($(this).children(".sub-menu").length) { $(this).children(".sub-menu").fadeOut(500); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="menu"> <li id="menu-item-12" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-12"> <a href="http://localhost:8080/testoir/sample-page/">Audiologia</a> <ul class="sub-menu" style="display: none; margin: 0px 40px; position: fixed; z-index: 10;"> <li id="menu-item-17" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-17"><a href="#">option1</a> <ul class="sub-menu" style="display: none; margin: 0px 40px; position: fixed; z-index: 10;"> <li id="menu-item-18" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-18"><a href="#">option2</a> </li> </ul> </li> </ul> </li> </ul> 

To get a array of elements with the same classname in pure Javascript: 要在纯Javascript中获得具有相同类名的元素数组:

var elements = document.getElementsByClassName('className');

To get the first element with the specified classname above: 要获取上面具有指定类名的第一个元素:

var element = elements[0];

If you wanted to get the second element it would be: 如果要获取第二个元素,它将是:

var element = elements[1];

暂无
暂无

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

相关问题 jQuery-如何从元素列表中选择具有类名称的元素 - jQuery- How to select a element with class name from a list of elements 如何只选择下课后的第一个元素? - How to select only the first element after class? 仅选择单击元素的“此”对象(而不选择同一类中的其他元素) - Select 'this' object of clicked element only (not the other elements within the same class) 如何使用jQuery从具有相同类名的许多元素中选择一个特定元素(div)? - How to select one specific element (div) from many elements with same class name in with jQuery? 在jQuery中,如何测试某个元素是否是同一类的许多元素中的第一个? - In jQuery, how to test whether an element is the first of many elements of same class? 如何在元素的类定义列表中选择第一类 - How to select first class in a list of class definition for an element jQuery-如何在文档区域中选择2个具有相同类名的元素? - Jquery - How to select 2 elements with same class name in a document area? 如何选择具有相同类名的所有元素? - How can I select all elements with the same class name? 如何选择具有相同类名 JavaScript 的多个元素 - How to Select multiple elements with the same class name JavaScript JavaScript函数仅适用于具有类名称的第一个元素,而不适用于具有该类的所有元素 - JavaScript function is only working for the first element with a class name, not all elements with that class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM