2010-12-07 181 views
0

我怎麼能做到這一點?鼠標懸停元素

if($('.element').mouseover() == true) 
{ 
} 
else 
{ 
} 

我想知道什麼時候如果沒有做別的事情,那麼這個元素就超過了元素。

以下是現在可用的完整代碼。我擺脫的if語句...

$('.product_image').hover(function(){ 
    var image = $(this).attr('class'); 
    image = '.' + image.substring(14); 
    $(image).fadeIn(); 
}); 
$('.product_disc').mouseleave(function(){ 
    $('.product_disc').fadeOut(); 
}); 
+0

讓我們知道你想要做什麼,已經有很多可用於鼠標懸停的方法,不需要檢查它是否爲鼠標懸停或者沒有其他的事情發佈你的HTML如果有東西不是工作 – kobe 2010-12-07 22:54:03

回答

2

我使用這個模式很多工作的例子:

$('.element').mouseenter(function() { 
    $(this).addClass('hover'); 
}).mouseleave(function(){ 
    $(this).removeClass('hover'); 
}); 

現在你可以使用is method,看看是否鼠標在元件。

有一個原因使用mouseenter與mouseout - 它必須與嵌套元素。你可以看到here

+0

我想mouseleave()的行爲與mouseout()不同。我用了,所有的問題都消失了。謝謝。 – JamesTBennett 2010-12-07 23:03:03

0
$(".element").mouseenter(function() { 

    alert('Mouse over the element'); 

}).mouseleave(function() { 

    alert('Mouse out'); 

}); 
0

多了一個最新的one..which是elagent使用

$(".element").hover(
    function() { 
    //this is mouseover 
    }, 
    function() { 
    //this is mouseout 
    } 
); 
0

是jQuery使用不同是什麼通常會寫的語法。他們的標籤行過去像「它會改變你寫JavaScript代碼的方式」。你必須像這樣使用mouseover():

$('.element').mouseover(function() { 
    // Use $(this) to access the element where the mouse is entering. 
}).mouseout(function() { 
    // Use $(this) to access the element where the mouse is exiting. 
}); 

還要注意類似的mouseenter()和mouseleave()方法。在這裏的官方文檔:http://api.jquery.com/mouseover/