现在正做个东西,是利用jquery的hover,来实现鼠标停留某处,一旦停留超过2秒就触发一个事件,不到2秒鼠标移走的话就不触发。一直没思路,求教!!!鸡鸡鸡啊!!!
解决方案 »
- 请教这个正则表达式如何写?
- ~javascript 的一句代码。请高手指路
- IE7中如何通过javascript捕获标签页关闭。
- 有没有好一点的可以快速入门的js教程,短小一点的,最好每章后面能带个习题的那种
- 【菜鸟求助】怎样向父页面传递加载分享变量?
- 在网页中打开一个文本附件,是否可以使网页中的title(标题)是可以控制的?
- 帮帮啊,各位高手
- 高手挑战---JS中如何判断ACTIVEX控件是否正确下载?
- 在js中写java小脚本,怎么在java小脚本中获取js变量的值
- 下面的javascript代码中,如何改动才能使index的值是2?(二维数组的难题哦!)
- ext 如何让grid其中一列的combox为只读
- 求ext中up方法的意思
如果页面上需要这个效果的地方比较多,也可以放到对象的attribute里面。计时器标识就是一个int值。
2秒后调用实际执行函数,不到两秒就clear掉。试一下呗。具体我也没做过。
str = setInterval(function () {
//绑定out事件
clearInterval(str);
}, 2000);
<!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" xml:lang="zh">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<style type="text/css">
div{width:100px;height:100px;border:solid 1px red;}
</style>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
var timer = null;
$(function(){
$("#test").hover(
function(){
$(this).html("悬停");
timer=setTimeout(function(){alert("hello")},2000);
},
function(){
if(timer)
clearTimeout(timer);
$(this).html("测试");
});
});
</script>
</head>
<body>
<div id="test">
测试
</div>
</body>
</html>
var timer;
$("input:button").hover(function() {
var self = this;
timer = setTimeout(function() {
timer = 0;
$(self).css("color", "red");
}, 2000);
}, function() {
if (timer) {
clearTimeout(timer);
timer = 0;
return;
}
$(this).css("color", "");
});
});
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var timeout = '';
$('p').mouseover(function(){
timeout = setTimeout(function(){
alert('2秒')
},2000);
})
$('p').mouseout(function(){
clearTimeout(timeout)
})
})
</script>
<style type="text/css">
</style>
</head><body>
<p>11111111111111111111111111</p>
</body></html>
$(this).html("悬停");
timer=setTimeout(function(){alert("hello")},2000);
},
function(){
if(timer)
clearTimeout(timer);
$(this).html("测试");
});
。同时发生了。我要的是2秒后第一个function发生,当鼠标离开后第二个function发生。
<head>
<script type="text/javascript" src="F:\workspace\JavaDemo\js\jquery-1.4.3.js"></script>
<script type="text/javascript">
$(function(){
var timer;
$("td[name='two']").each(
function(i){
$(this).hover(
function (){
timer=setTimeout(function(){timer = 0;alert("begin")},2000);
},
function(){
if(timer) {
clearTimeout(timer);
timer = 0;
return;
};
alert("end");
}
);
}
);
});
</script>
</head>
<body>
<table border="1">
<tr>
<td>aaaaaaaaaa</td><td name="two">bbbbbbbbb</td>
</tr>
<tr>
<td>cccccccccc</td><td name="two">ddddddddd</td>
</tr>
</table>
<body>
</html>
正常的:alert对话框会使dom失去hover状态。用其他方式测试。
将
$(this).html("测试");
放入if里就行了。直接改你这个。<html>
<head>
<script type="text/javascript" src="F:\workspace\JavaDemo\js\jquery-1.4.3.js"></script>
<script type="text/javascript">
$(function(){
var timer;
$("td[name='two']").each(
function(i){
$(this).hover(
function (){
var obj =$(this);
//外面嵌套一个function,不做任何操作,setTimeout里放你自己的处理
timer=setTimeout(function(){obj.css({"backgroundColor":"red"});},2000);
},
function(){
if(timer) {
clearTimeout(timer);
$(this).css({"backgroundColor":""});
};
}
);
}
);
});
</script>
</head>
<body>
<table border="1">
<tr>
<td>aaaaaaaaaa</td><td name="two">bbbbbbbbb</td>
</tr>
<tr>
<td>cccccccccc</td><td name="two">ddddddddd</td>
</tr>
</table>
<body>
</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>无标题文档</title></head><body onload="init()"><script language="javascript">var
bIn = false,
oHint;
oTimer;
function init()
{
oHint = document.getElementById("hinter");
hideHint();
}
function onOut()
{
bIn = false;
clearTimeout(oTimer);
}
function onOver()
{
bIn = true;
oTimer = setTimeout("onTime()", 2000);
}
function onTime()
{
if(bIn)
{
oHint.style.display = "";
}
}
function hideHint()
{
oHint.style.display = "none";
}
</script><table border="1" cellpadding="6" cellspacing="0" onmouseover="onOver()" onmouseout="onOut()">
<tr>
<td><p>你敢在这<br />
区域里停<br />
两秒吗?</p></td>
</tr>
</table>
<div style="position:absolute; display:inline; background:#999" id="hinter" onclick="hideHint()">你赢了<br>[关闭]</div>
</body>
</html>