简体   繁体   English

获取元素鼠标当前指向启用 Chrome 开发人员工具

[英]Fetch element mouse is currently pointing to with Chrome developer tools enabled

I am looking to fetch the element which gets selected when we right click on "Inspect Element" through Javascript.我希望通过 Javascript 右键单击​​“检查元素”来获取被选中的元素。

Since when we move mouse around with "Inspect" mode on, the area selected is displayed within the same web page on UI and gets modified by mouse movement, I am assuming that it should be accessible through js.由于当我们在“检查”模式下移动鼠标时,所选区域显示在 UI 上的同一网页中并通过鼠标移动进行修改,因此我假设它应该可以通过 js 访问。

One working solution would be to add mouseover event on all the DOM elements & use it to get the current element mouse is hovering to.一种可行的解决方案是在所有 DOM 元素上添加mouseover事件并使用它来获取鼠标悬停到的当前元素。 But if Chrome developer tools already is doing something similar, I'd like to use it to get the element, instead of applying eventlisteners to each element on page.但是,如果 Chrome 开发人员工具已经在做类似的事情,我想用它来获取元素,而不是将事件eventlisteners器应用于页面上的每个元素。

Basically I'm thinking of calling:基本上我想打电话:

inspect(document.body);

and then dynamically get the element mouse is hovering on through js.然后通过js动态获取鼠标悬停的元素。 But I'm not sure how to access it or is it even possible.但我不确定如何访问它,或者甚至可能。

Events bubble up, so it'd only take one event listener on the document.事件冒泡,所以它只需要一个事件监听器在文档上。 The target will be the most specific going towards least specific. target将是最具体的,朝着最不具体的方向发展。

document.body.addEventListener('click', function(e) {
  console.log(e.target.id);
})

http://jsbin.com/wipinahopi/1/edit?js,console,output http://jsbin.com/wipinahopi/1/edit?js,console,output

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM