开发者

menu find a child of li element with jQuery

What I need to add a class on hover to 开发者_如何学编程the a tag with a menu below is the menu. Any ideas?

<ul id="nav">

<li><a href="">Home</a></li>

<li><a href="">Another</a>

<ul>

<li><a href="">Sub</a></li> ...

$("#nav li ul li a").hover(
    function() {
         $(this).parent().parent().parent().addClass('current');

    },
    function() {
        $(this).parent().parent().parent().removeClass('current');      
        //alert();          
    }
);


$("#nav a").hover(
    function() { $(this).closest("a").addClass("current"); },
    function() { $(this).closest("a").removeClass("current"); }
);


You can achieve this without jQuery by using css psuedo classes. All elements in html gain a psuedo class of :hover when the mouse is over them.

To select them in CSS:

#nav li ul li:hover {
    // Your style here.
}


This method is what you need http://api.jquery.com/closest/ god bless jQuery


Might but just quicker to do this

$(document).ready(function() {
    $('#nav a:hover').parents('div > ul > li:hover > a').addClass('current');
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜