I have the next menu structure.
<li class="menu-422"><a href="item-1" title="">Item 1</a></li>
<li class="menu-444"><a href="item-2" title="">Item 2</a></li>
<li class="menu-449"><a href="item-3" title="">Item 3</a></li>
<li class="menu-452"><a href="item-4" title="">Item 4</a></li>
In this structure the items (the a-tag) have background. I want to change the background of the next item on hover event. If I'm over the Item 2 I want to change the background of the Item 3. I tried it with jQuery, but I didn't find the appropriate code.
jQuery("#mymenu li a").hover(function() {
jQuery(this).next("li a").css('background', 'none');
}
I tried it with nextAll, with full selector path, but I can't select the next element.
The ("element + next") selector selects the "next" element of the specified "element". The "next" element must be placed right after the specified "element" to be selected.
The parents() is an inbuilt method in jQuery which is used to find all the parent elements related to the selected element. This parents() method in jQuery traverse all the levels up the selected element and return that all elements.
jQuery closest() Method The closest() method returns the first ancestor of the selected element. An ancestor is a parent, grandparent, great-grandparent, and so on.
jQuery next() Method The next() method returns the next sibling element of the selected element. Sibling elements are elements that share the same parent. The DOM tree: This method traverse forward along the next sibling of DOM elements.
Upon hover, you want to go up to the parent li
, then use next()
to get to the next li
, then find('a')
inside of that li
:
$("#mymenu li a").hover(function() {
$(this).parent().next().find("a").css('background', 'none');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With