如何在鼠标hover时显示/隐藏div?
可能重复:
如何使用jquery在鼠标hover时显示/隐藏div?
我有这样的div:
foo hidden
我希望每当有人将鼠标移到parent
div上时显示parent
div,并且当他移动鼠标时,隐藏父div。
但是,如果我这样做:
$("#parent").mouseover(toggle); $("#parent").mouseout(toggle); function toggle() { console.log('here'); $("#child").toggle(); }
然后,每当我将鼠标移到#parent div上时,两个事件似乎都会被调用两次。 我怎样才能实现我想要的目标?
$("#parent").hover( function(){ $("#child").show(); }, function(){ $("#child").hide(); } )
添加css怎么样?
#parent #child {display:none;}
#parent:hover #child {display:block;}
同
foo hidden
在这种情况下,您不应该使用切换。 如果你总是想在mouseout上隐藏并在鼠标hover时显示,那么将它们定义为:)
$("#parent").mouseover(function() { $("#child").fadeIn(); // or .show() for no fading }); $("#parent").mouseout(function() { $("#child").fadeOut(); // or .hide() for no fading });
不要使用切换function!!
尝试这样的事情:
$("#parent").hover(function(){ $("#child").show(); }, function(){ $("#child").hide();
})
这应该可行!!
短:
$("#parent").hover(function () { $("#child").toggle(); });
演示。
注意:您可以使用fadeToggle
或slideToggle
toggle
。
你可以尝试这样的.hover(),
$('#divid').hover( function(){ //mouseenter function }, function(){ //mouseleave function } );
你应该使用这样的东西:
$("#parent").mouseover(function(){ $('#child').show(); }); $("#parent").mouseout(function(){ $('#child').hide(); });
jsFiddle示例: http : //jsfiddle.net/phcwW/
foo
Js方法:
function callMouseOver(){ document.getElementById("child").style.display = "inline"; } function callMouseOut(){ document.getElementById("child").style.display = "none"; }