2010-09-23 19 views
0

代碼:jQuery的優化懸停事件和圖像

$('.featured').hover(
    function() { 
     $(this).addClass('active'); 
     $('img',this).fadeTo('slow', 0.5, function() {}) 
    }, 
    function() { 
     $(this).removeClass('active'); 
     $('img',this).fadeTo('slow', 1, function() {}) 
    }); 

我怎麼能提高呢?

我記得有人告訴我,一旦不使用

$('img', this) ..

,但我無法弄清楚如何內DIV正在上空盤旋任何其他方式訪問圖像。

謝謝!

回答

2

您可以使用.find(),像這樣:

$('.featured').hover(function(event) { 
    $(this).addClass('active').find('img').fadeTo('slow', 0.5); 
}, function() { 
    $(this).removeClass('active').find('img').fadeTo('slow', 1); 
}); 

此發現你徘徊在元素中的任何元素<img> ...跳過幾個步驟$(selector, context)必須採取弄清楚它是真的一個$(context).find(selector)呼叫。也不需要動畫回調......它們是可選的,所以如果你在做任何事情,就把它們關掉。

+0

現貨。謝謝。 – Ross 2010-09-23 15:13:12