【发布时间】:2010-06-02 10:13:50
【问题描述】:
<html>
<head>
<script>
function addEvent( obj, type, fn ) {
if ( obj.attachEvent ) {
obj['e'+type+fn] = fn;
obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
obj.attachEvent( 'on'+type, obj[type+fn] );
} else
obj.addEventListener( type, fn, false );
}
</script>
</head>
<body>
<!-- HTML for example event goes here -->
<div id="mydiv" style="border: 1px solid black; width: 100px; height: 100px; margin-top: 10px;"></div>
<script>
// Script for example event goes here
addEvent(document.getElementById('mydiv'), 'contextmenu', function(event) {
display_short('right-clicked (contextmenu)');
});
function display_short(str)
{
clearTimeout();
document.getElementById('mydiv').innerHTML = str;
if (str != '')
alert("hello");
setTimeout("display_short('abcd')", 1000);
}
</script>
</body>
</html>
右键单击 div 区域时,在 IE 和 FF 中的行为不同。在 ff 中 display_short 将每 1 秒调用一次,即使您不释放右键单击,但在 IE 中,如果您不释放右键单击,它将不会调用 display_short。
但我希望在 IE 中它应该每 1 秒调用一次 display_short,如果你也不释放的话。 有什么解决办法吗?
【问题讨论】:
-
在两种浏览器中对我来说都是一样的。
oncontextmenu仅在右键释放时触发,而不是按下。您对clearTimeout()的使用毫无意义,因为您没有传入计时器句柄(由setTimeout函数返回);无论如何,如果超时已经触发,则不需要清除它。
标签: javascript