简体   繁体   English

document.querySelectorAll与伪类:悬停不在FF或IE中工作

[英]document.querySelectorAll with the pseudo-class :hover not working in FF or IE

I want to get the element under the cursor. 我想获取光标下的元素。 When I use document.querySelectorAll(":hover"); 当我使用document.querySelectorAll(":hover"); , it works well in Chrome but it doesn't work in Firefox or IE. ,它在Chrome中运行良好,但在Firefox或IE中无效。

It might be because I use it inside a eventListener in a Google maps. 这可能是因为我在Google地图中的eventListener中使用它。 Here how I use it. 我在这里如何使用它。

google.maps.event.addListener(polygon,"mouseout",function(){
  elementHover = document.querySelectorAll( ":hover" );
  alert(elementHover[elementHover.length-1].id);
});

In Chrome it gives me the id of the element I'm hovering with the cursor, but I get nothing in IE or FF. 在Chrome中它给了我用光标悬停的元素的id,但我在IE或FF中什么都没得到。

Is there a reason you're using mouseout instead of mouseover? 你有没有理由使用mouseout而不是mouseover? It seems like, depending on how the browser resolves it (does it fire the event before you leave, or just after you leave, the object that is listening for the event?) that could cause some consternation. 似乎,取决于浏览器如何解决它(它是在你离开之前触发事件,还是刚刚离开之后,正在侦听事件的对象?),这可能会引起一些惊愕。 Is there a reason you're not just passing in the Event object to get the object you're leaving, rather than hoping that a selector will fire? 有没有理由你不只是传递Event对象来获取你要离开的对象,而不是希望选择器会触发?

According to Google's docs ( https://developers.google.com/maps/documentation/javascript/events#EventArguments ), you can pass the event object into the function: 根据Google的文档( https://developers.google.com/maps/documentation/javascript/events#EventArguments ),您可以将事件对象传递到函数中:

google.maps.event.addListener(polygon,"mouseout",function(evt){
  // get the target from the mouseout event, something like this:
  elementHover = evt.target;
  alert(elementHover[elementHover.length-1].id);
});

I can't test this at the moment, so you'll probbly have to fiddle with it and read into Google's docs to make sure that the event you're looking at gives you a reference to the object that it came from (you might even be able to use "this" instead of evt.target, depending on what gets passed into context). 我目前无法对此进行测试,因此您可能不得不调整它并阅读Google的文档以确保您正在查看的事件为您提供对其来自的对象的引用(您可能甚至可以使用“this”而不是evt.target,具体取决于传递给上下文的内容)。 However, :hover is still a semi-flighty beast, and depending on the order in which events are resolved, it's quite possible that you only see it in Chrome because it fires off the events differently than FF and IE. 但是,:悬停仍然是一个半飞行的野兽,并且根据事件解决的顺序,你很可能只在Chrome中看到它,因为它以不同于FF和IE的方式触发事件。

Good luck! 祝好运!

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

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