2012-05-31 70 views

回答

0

这是你在找什么?

$('.submenu-link').hover (function (e) { 
     $(this).parent().parent().children('a').css('backgroundColor', 'red'); 
    }, function (e) { 
     $(this).parent().parent().children('a').css('backgroundColor', 'white'); 
    }); 

的jsfiddle:http://jsfiddle.net/fGGrf/5/

+0

相似,但应用和保持变化,如果父母是活动页面。 – kqualters

+0

好的,那么你需要制作一个你切换的CSS类。 – Hidde

+0

谢谢我有一个.current类,我忘了在小提琴中。问题在于javascript根据当前页面的href是否与菜单的任一级别相对应来添加它,如果这样做有意义 – kqualters

0

试试这个:

$(document).ready(function(){ 
var dropdown = $('.dropdown'); 
var link= $('.submenu-link'); 

var cur = "www.domain.com/overview.php" // document.location.href ? document.location.href : document.location; 
cur = cur.split('/' , -1); 

    $('li a').each(function() { 
     if ($(this).attr('href') == '/' + cur[1]) { 
      $(this).parent().addClass('active'); 
     } 
    }) 

}); 

http://jsfiddle.net/fGGrf/6/

+0

这非常接近,只是将其仅添加到当前链接,它需要被添加到顶级li元素 – kqualters

+0

@ kq9873你可以用'$(this).closest('。parent')。addClass('active');'http:// jsfiddle来选择顶级元素。净/ fGGrf/7/ – undefined

+0

jQuery的挫败我没有尽头我这个不是 “$(函数(){ \t \t VAR CURRENT_PAGE = win.location.pathname || '',路径去; \t \t $( 'a','#nav')。each(function(i,el){ \t \t(= /'+')。 \t \t \t如果(路径== CURRENT_PAGE){ \t \t \t \t $(EL)。家长(+ parent_class ' ')。首先()addClass(' 激活')。 \t \t \t \t return false; \t \t \t} \t \t}); \t});' – kqualters