Element.prototype.match不匹配裸:p seudoclass



出于某种原因,Element.prototype.matchs对于完全有效的CSS选择器(:hover(返回false:

let n = /* an HTMLAnchorElement whose state is forced to :hover */;
console.log(n.matches(':hover')); // false
console.log(n.matches('a:hover')); // true
console.log(n.matches('*:hover')); // false
  • 找到一个 DOM 节点后,我在开发工具控制台中运行了该代码,使用"元素"面板强制其状态为 :hover,然后将n设置为$0
  • n可以是页面中的任何 HTML dom 节点 - 它不限于锚元素。

此外,使用document.querySelector表现出相同的行为 - 但仅在我的本地测试环境中。例如,https://google.com/为所有这些测试返回 true。

这是因为我在页面顶部缺少<!DOCTYPE html>。有关详细信息,请参阅 https://developer.mozilla.org/en-US/docs/Web/HTML/Quirks_Mode_and_Standards_Mode。

最新更新