获得鼠标指向的页面元素

类F12鼠标选中功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
window.onmousemove = track_mouse;
var last_element = null
function track_mouse(event){
var elementMouseIsOver = get_current_element(event)
if (elementMouseIsOver === last_element) {
return;
}
if (last_element != null) {
remove_hightlight(last_element);
}
highlight(elementMouseIsOver);
last_element = elementMouseIsOver;
}
function get_current_element(event){
var x = event.clientX;
var y = event.clientY;
var element = document.elementFromPoint(x, y);
return element;
}
function highlight(element){
element.style.outline = '#f00 solid 2px';
}
function remove_hightlight(element){
element.style.removeProperty('outline');
}
0%