我的鼠标悬停和鼠标输出功能有问题。当我鼠标悬停一个链接时,它显示一个隐藏的<div>
,当我鼠标输出div时,它隐藏了div。问题是,如果我将鼠标悬停在链接上,那么我将鼠标移动到不在div上方的其他地方,div将不会消失。
如果我使用链接的mouseout事件来设置div的可见性,那么我将无法将鼠标悬停在div上。
这是我的HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
Untitled Document
</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#show_div").mouseover(function() {
$("#hello").css('visibility', 'visible');
});
$("#hello").mouseover(function() {
$("#hello").css('visibility', 'visible');
});
$("#hello").mouseout(function() {
$("#hello").css('visibility', 'hidden');
});
});
</script>
</head>
<body>
<br/>
<br/>
<br/>
<br/>
<a id="show_div" href="#">Link text</a>
<div id="hello" style="visibility:hidden;">
<ul>
<li>
Coffee
</li>
<li>
Tea
</li>
<li>
Milk
</li>
</ul>
</div>
<br/>
<br/>
</body>
</html>
答案 0 :(得分:2)
我使用setTimeout函数来更改css属性。将setTimeout的时间间隔设置为~333-500毫秒,并将Div的鼠标悬停设置为清除超时。然后,在div本身的mouseout上,再次设置计时器:)
实施例/答案:
// timer for hiding the div
var hideTimer;
// show the DIV on mouse over
$("#show_div").mouseover(function() {
// forget any hiding events in timer
clearTimeout( hideTimer );
$("#hello").css('visibility', 'visible');
});
$("#hello").mouseover(function() {
clearTimeout( hideTimer );
$("#hello").css('visibility', 'visible');
});
// set a timer to hide the DIV
$("#show_div").mouseout(function() {
hideTimer = setTimeout( hideHello, 333 );
});
$("#hello").mouseout(function() {
hideTimer = setTimeout( hideHello, 333 );
});
// hides the DIV
function hideHello() {
$("#hello").css('visibility', 'hidden');
}
答案 1 :(得分:2)
将整个东西放在一个容器中,并将鼠标事件放在:
上试一试: http://jsfiddle.net/hGTPp/
HTML
<div id='container'>
<a id="show_div" href="#">Link text</a>
<div id="hello" style="visibility:hidden;">
<ul>
<li>
Coffee
</li>
<li>
Tea
</li>
<li>
Milk
</li>
</ul>
</div>
</div>
的jQuery
$("#container").mouseover(function() {
$("#hello").css('visibility', 'visible');
});
$("#container").mouseout(function() {
$("#hello").css('visibility', 'hidden');
});
答案 2 :(得分:0)
请改用.hover()。它允许您指定handlerIn
和handlerOut
个事件。 E.g。
<强>的jQuery 强>
<script type="text/javascript">
$(document).ready(function() {
$("#linkdiv").hover(function() {
$("#hello").show();
}, function() {
$("#hello").hide();
});
});
</script>
HTML
<div id="linkdiv">
<a id="show_div" href="#">Link text</a>
<div id="hello" style="display: none;">
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
</div>
</div>
修改:在Nick发表评论后稍稍更改了代码。谢谢尼克。
答案 3 :(得分:0)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
Untitled Document
</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#show_div").hover(
function(){
$('#hello').show();
},
function(){
$('#hello').hide();
});
});
</script>
</head>
<body>
<br/>
<br/>
<br/>
<br/>
<div id="show_div">
<a href="#">Link text</a>
<ul id="hello" style="display:none;">
<li>
Coffee
</li>
<li>
Tea
</li>
<li>
Milk
</li>
</ul>
</div>
<br/>
<br/>
</body>
</html>
答案 4 :(得分:0)
CSS解决方案:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
#link{
display:inline-block;
overflow:hidden;
height:20px;
}
#link:hover{
height:auto;
}
</style>
</head>
<body>
<br />
<div id="link">
<a href="#">Link text</a>
<ul>
<li>
Coffee
</li>
<li>
Tea
</li>
<li>
Milk
</li>
</ul>
</div>
</body>
</html>
答案 5 :(得分:0)
$(document).ready(function()
{
var timer;
$("#show_div").hover(
function ()
{
$('#hello').show();
},
function()
{
timer = setTimeout(function(){$('#hello').hide();}, 5000);
}
);
$("#hello").hover(
function ()
{
clearTimeout(timer);
},
function()
{
$('#hello').show();
}
);
}