2010-11-23 38 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","")); 
    });