[英]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.