Jquery 鼠标悬停在元素上
为什么我不能这样做Jquery 鼠标悬停在元素上,jquery,mouseover,Jquery,Mouseover,为什么我不能这样做 if($('.element').mouseover() == true) { } else { } 我想知道如果一个元素没有做其他事情,mosue什么时候结束了 下面是现在可以工作的完整代码。我去掉了if语句 $('.product_image').hover(function(){ var image = $(this).attr('class'); image = '.' + image.substring(14); $(image).fadeIn();
if($('.element').mouseover() == true)
{
}
else
{
}
我想知道如果一个元素没有做其他事情,mosue什么时候结束了
下面是现在可以工作的完整代码。我去掉了if语句
$('.product_image').hover(function(){
var image = $(this).attr('class');
image = '.' + image.substring(14);
$(image).fadeIn();
});
$('.product_disc').mouseleave(function(){
$('.product_disc').fadeOut();
});
还有一个最新的..很方便使用
$(".element").hover(
function () {
//this is mouseover
},
function () {
//this is mouseout
}
);
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()方法。请参阅此处的官方文档:。以下是一个工作示例: 这使用了jQuery的函数 祝你好运 编辑:下面是一个使用
我经常使用这种模式:
$('.element').mouseenter(function() {
$(this).addClass('hover');
}).mouseleave(function(){
$(this).removeClass('hover');
});
现在,您可以使用查看鼠标是否在元素上
使用mouseenter vs mouseout是有原因的——它与嵌套元素有关。你可以看到让我们知道你想做什么,已经有很多方法可用于鼠标悬停,没有必要检查是否有鼠标悬停//还有一件事,如果某些东西不起作用,请发布你的html我猜mouseleave()的行为与mouseout()不同。我用它解决了所有的问题。谢谢
$('.element').mouseenter(function() {
$(this).addClass('hover');
}).mouseleave(function(){
$(this).removeClass('hover');
});