gpt4 book ai didi

Javascript onHover 事件

转载 作者:IT王子 更新时间:2023-10-29 03:12:34 26 4
gpt4 key购买 nike

是否有一种规范的方法可以使用现有的 onmouseover、onmouseout 和某种计时器来设置 JS onHover 事件?或者当且仅当用户将鼠标悬停在元素上一定时间时才触发任意函数的任何方法。

最佳答案

这样的事情怎么样?

<html>
<head>
<script type="text/javascript">

var HoverListener = {
addElem: function( elem, callback, delay )
{
if ( delay === undefined )
{
delay = 1000;
}

var hoverTimer;

addEvent( elem, 'mouseover', function()
{
hoverTimer = setTimeout( callback, delay );
} );

addEvent( elem, 'mouseout', function()
{
clearTimeout( hoverTimer );
} );
}
}

function tester()
{
alert( 'hi' );
}

// Generic event abstractor
function addEvent( obj, evt, fn )
{
if ( 'undefined' != typeof obj.addEventListener )
{
obj.addEventListener( evt, fn, false );
}
else if ( 'undefined' != typeof obj.attachEvent )
{
obj.attachEvent( "on" + evt, fn );
}
}

addEvent( window, 'load', function()
{
HoverListener.addElem(
document.getElementById( 'test' )
, tester
);
HoverListener.addElem(
document.getElementById( 'test2' )
, function()
{
alert( 'Hello World!' );
}
, 2300
);
} );

</script>
</head>
<body>
<div id="test">Will alert "hi" on hover after one second</div>
<div id="test2">Will alert "Hello World!" on hover 2.3 seconds</div>
</body>
</html>

关于Javascript onHover 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/262740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com