检测鼠标光标类型在元素上的变化

Detecting mouse cursor type change over element

可以在没有预定义光标样式的情况下获取当前光标类型,例如当鼠标经过文本时,link..

类似的东西:

document.addEventListener('mouseover', (e) => {
  console.log(e.'cursorType')
});

我想进入 console.log 光标状态,例如:指针、文本、移动、等待...

我在 jQuery 中找到了某种解决方案,但我正在寻找纯 vanilla JS 中的解决方案

感谢您的回答

您可以像这样从 CSS 属性“光标”获取它:

document.addEventListener('mouseover',function(e){
    var cursor = e.target.style.cursor;
    console.log(cursor);
},false);

尝试记录 e.target.style.cursor。我认为这会在 DOM 元素的鼠标悬停时为您提供光标类型。

因为它可能没有被内联定义,所以你需要计算样式:

我用的是Click here,因为比较容易demo

document.addEventListener('click', e => {
  const tgt = e.target;
  const inline = tgt.style.cursor || "Not defined"
  const computed = window.getComputedStyle(tgt)["cursor"]
  console.log("Inline: ",inline,"Computed: ",computed)
});
.help { cursor: help }
<span style="cursor:pointer">Inline Pointer</span>
<hr>
<span class="help">CSS help</span>