2010-11-23 104 views
0

说,你有这样的代码:jQuery的悬停多个项目

$("#sub_nav_home1").hover(function() { 
     $("#homepage").removeClass(); 
     $("#homepage").addClass("homepage1"); 
    }); 
    $("#sub_nav_home2").hover(function() { 
     $("#homepage").removeClass(); 
     $("#homepage").addClass("homepage2"); 
    }); 
    $("#sub_nav_home3").hover(function() { 
     $("#homepage").removeClass(); 
     $("#bhomepage").addClass("bhomepage3"); 
    }); 

    <ul class="nav sub_nav_home"> 
     <li id="sub_nav_home1"><a href="#"><span>LINK1</span></a></li> 
     <li id="sub_nav_home2"><a href="#"><span>LINK2</span></a></li> 
     <li id="sub_nav_home3"><a href="#"><span>LINK3</span></a></li> 
    </ul> 

有没有办法了,我们只能做悬停jQuery的功能一次,而不是每一个环节?

希望这是有道理的

干杯

+1

你能详细说“只有一次”吗? – 2010-11-23 23:31:02

+0

同意@Brad。你在寻找[`.one()`](http://api.jquery.com/one)吗? – 2010-11-23 23:34:12

回答

1

犯错,我能想到的是最快的方法(这将是动态的一样):

$("#sub_nav_home a").hover(function() { 
     $("#homepage").removeClass().addClass("homepage"+$(this).parent().attr('id').split('sub_nav_home')[1]); 
    }); 

    <ul class="nav sub_nav_home"> 
     <li id="sub_nav_home1"><a href="#"><span>LINK1</span></a></li> 
     <li id="sub_nav_home2"><a href="#"><span>LINK2</span></a></li> 
     <li id="sub_nav_home3"><a href="#"><span>LINK3</span></a></li> 
    </ul> 

这将使课堂主页[这是父母的身份证号码] ie homepage1,home第2页等

--UPDATE--

这应该为你工作(但我没有测试一下吧!只是让我知道,如果它不起作用)

//find the first li and add a class of current to start the loop 
$('.sub_nav_home li:first').addClass('current'); 
//Here we set the loop 
setInterval(function(){ 
    //Here we are checking if there IS a next item. If there IS it'll return 1 
    //which will make this if() true (1 > 0) 
    if($('.current').next().length > 0){ 
     //Here we grab the current .current, remove the class, get the next item 
     //and then add .current to that 
     $('.current').removeClass('current').next().addClass('current'); 
    } 
    else{ 
     //If the if() fails (returns 0 [no next() item]) we'll get the current 
     //.current, remove the class, get the parent, find the first item in the 
     //parent (first <li> of the <ul>) and add a class of current 
     $('.current').removeClass('current').parent().find(':first').addClass('current'); 
    } 
},3000) //3000 = 3 seconds 

p.S.如果这对你有用,一定要给我一个投票;)

0

添加类每个李:

<li class="c1" id="sub_nav_home1"><a href="#"><span>LINK1</span></a></li> 

$('ul.nav sub_nav_home li').hover(function() { 
     $("#homepage").removeClass(); 
     $("#bhomepage").addClass("bhomepage" + this.className.replace("c","")); 
    });