2010-12-07 182 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/