Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add class to child of this

This code is not working. this+'>a' isn't a valid syntax. So, how can I add/remove a class in a child of this? In this case an a element.

 jQuery(function ($) {
        $("nav.menu>ul>li").hover(
            function () {
               $(this+'>a').addClass("hover_triangle");//error
            },

            function () {
               $(this+'>a').removeClass("hover_triangle");//error
            });
    }); 

I can't do nav.menu>ul>li>a because will select all a elements in menu.

like image 721
user1311784 Avatar asked Oct 29 '12 01:10

user1311784


People also ask

How to add class for child in jQuery?

$('. rotation ul'). addClass('image_rotation'); The above code will add a class of image_rotation to every ul that is a descendant of any element with the class rotation .

How do I add a class to my child Nodejs?

You can simply document. querySelectorAll to select the list. use "firstElementChild" to get first child node and add class.


1 Answers

$(this).children('a').addClass('hover_triangle');

and with the full code:

jQuery(function($) {
    $('nav.menu>ul>li').hover(function() {
       $(this).children('a').addClass('hover_triangle');
    },function() {
       $(this).children('a').removeClass('hover_triangle');
    });
}); 
like image 120
inhan Avatar answered Sep 24 '22 00:09

inhan