当鼠标悬停时,获取任何标签的ID [英] Getting id of any tag when mouseover
问题描述
有没有人知道如何在鼠标结束时获得任何元素的id?
does anyone know how i can get the id of any element when the mouse is over ?
我想在元素(标签)上显示一个div(框) )鼠标已经结束了。
我无法修改标签以包含mousover事件。我想要一个全局回调或类似的东西来标记鼠标指针下的id。
I want to show a div (box) over the elements (tags) the mouse is over. I cannot modify the tags to include a mousover event. I want a global callback or something like that to have the id of the tag which is under the mouse pointer.
谢谢!
推荐答案
你的意思是你想要 onmouseover
事件的目标,因此您可以访问元素的属性:
You mean you want the target of the onmouseover
event, so you can access the element's properties:
<script>
document.onmouseover = function(e) {
console.log(e.target.id);
}
</script>
看看事件属性以跨浏览器方式获取目标(以下示例来自上述网站):
Take a look at Event Properties for a cross-browser way to get the target (the following example is from the aforementioned website):
function doSomething(e) {
var targ;
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode;
}
所以把它们放在一起:
document.onmouseover = function(e) {
var targ;
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode;
console.log(targ.id);
}
这篇关于当鼠标悬停时,获取任何标签的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!