创建一个覆盖整个页面的垫片,但允许在后台发生鼠标悬停事件
我需要为整个页面创建一个垫片来捕获单击事件,但垫片不得阻止页面上其他 html 元素上的其他事件,例如鼠标悬停等。
我有一个可行的解决方案,但我遇到了一些问题: 每当我单击页面时,如果直接单击某些文本,填充程序的单击事件不会触发(页面上没有事件取消)。
onclick问题仅出现在IE中。
在 FF 和 Chrome 中,存在鼠标悬停事件未在后台触发的问题,因此我当然也会感谢任何有关解决该问题的提示...
这是一个演示该问题的非常基本的示例。
有什么想法吗? :-)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style type="text/css">
body
{
font-size:20px;
height:100%;
}
#shim
{
position: absolute;
padding: 0;
margin: 0;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
zindex: 1;
}
#trace
{
border: 1px solid #ccc;
height: 100px;
}
</style>
<script type="text/javascript">
function trace(e) {
e = e || event;
document.getElementById('trace').innerHTML = "Hovered innerHTML: " + (e.target || e.srcElement).innerHTML;
}
</script>
</head>
<body>
<div id="shim" onclick="alert('shim clicked')">
</div>
<table border="1" onmouseover="trace(event)">
<tr>
<td>
AAAAAA AAAAAA
</td>
<td>
BBBBBB BBBBB
</td>
<td>
CCCCCCCC CCCCC
</td>
<td>
DDDDDD DDDDDDDDDD
</td>
<td>
EEEEEE EEEE
</td>
<td>
FFFFFF FFFFF
</td>
<td>
GGGG GGGGGGGG
</td>
<td>
EEEE EEEE
</td>
<td>
FFFF FFFFFF
</td>
</tr>
</table>
<div id="trace">
</div>
</body>
</html>
I need to create a shim for the entire page to capture click events, but the shim must not stop other events like mouseover etc on the other html elements on the page.
I have solution that sort of works, but I am having a couple of problems with it:
Whenever I click on the page, if I click directly on some text, the click event of the shim does not fire (there is no event-cancelling on the page).
The onclick problem only occurs in IE.
In FF and Chrome there is a problem with the mouseover events not being fired in the background, so I would of course appreciate any tips on solving that as well...
Here is a very basic example demonstrating the problem.
Any ideas? :-)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style type="text/css">
body
{
font-size:20px;
height:100%;
}
#shim
{
position: absolute;
padding: 0;
margin: 0;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
zindex: 1;
}
#trace
{
border: 1px solid #ccc;
height: 100px;
}
</style>
<script type="text/javascript">
function trace(e) {
e = e || event;
document.getElementById('trace').innerHTML = "Hovered innerHTML: " + (e.target || e.srcElement).innerHTML;
}
</script>
</head>
<body>
<div id="shim" onclick="alert('shim clicked')">
</div>
<table border="1" onmouseover="trace(event)">
<tr>
<td>
AAAAAA AAAAAA
</td>
<td>
BBBBBB BBBBB
</td>
<td>
CCCCCCCC CCCCC
</td>
<td>
DDDDDD DDDDDDDDDD
</td>
<td>
EEEEEE EEEE
</td>
<td>
FFFFFF FFFFF
</td>
<td>
GGGG GGGGGGGG
</td>
<td>
EEEE EEEE
</td>
<td>
FFFF FFFFFF
</td>
</tr>
</table>
<div id="trace">
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道是否可以解决您的问题,但我几周前做了一些对我有用的事情。
基本上,我创建一个全局事件处理程序,在文档元素中注册我需要的所有事件,然后使用 event.srcElement 或 event.element,我能够首先获取触发事件的元素。根据事件和元素,我可以决定做什么。
I do not know if it is possible to fix your problem, but I have done something like a few weeks ago that works for me.
Basically, I create a global event handler, registering all events I need in the document element and then, using event.srcElement or event.element, I was able to get the element that fires the event in the first place. Based on the event and the element, I can decide what to do.