Why doesn't getComputedStyle work with pseudo classes like :hover?

后端 未结 2 861
慢半拍i
慢半拍i 2020-12-06 10:45

The function window.getComputedStyle is supposed to be able to get the computed style of pseudo classes like :hover, according to the documentation.

It\

2条回答
  •  Happy的楠姐
    2020-12-06 11:36

    Note that there is a way to do this at least for Chrome with the remote debugging port turned on. Basically you need to use a WebSocket, connect to the remote debugger url, then issue the following commands:

    send  { id: 1, method: "Inspector.enable" }
    send  { id: 2, method: "DOM.enable" }
    send  { id: 3, method: "CSS.enable" }
    send  { id: 4, method: "DOM.getDocument" }
    send  { id: 5, method: "DOM.querySelector", params: { nodeId: 1, selector: <> } }
    nodeId = response.result.nodeId
    send  { id: 6, method: "CSS.forcePseudoState", params: { nodeId:, forcedPseudoClasses: [ "hover" <> ] } }
    

    Then you can use the regular getComputedStyle, or the CSS.getComputedStyleForNode debugger API.

提交回复
热议问题