【问题标题】:Is there any way to get mouse/cursor icon on standard html elements有什么方法可以在标准 html 元素上获取鼠标/光标图标
【发布时间】:2017-08-29 03:53:07
【问题描述】:

有没有办法确定鼠标在没有css光标样式设置的标准html元素上时的样式或图标?例如,鼠标图标在标准 html 按钮上时从箭头变为手形,或在标准文本输入上时从箭头变为文本输入图标。

此代码仅在元素具有 style.cursor 设置时才有效。

document.addEventListener("mousemove", function(event) {
    //console.log("mouse is down")
     var x = event.clientX, y = event.clientY,
    elementMouseIsOver = document.elementFromPoint(x, y);
  console.log("element="+elementMouseIsOver+" cursor="+elementMouseIsOver.style.cursor)
    })

还有其他方法可以获取标准 html 项的鼠标图标吗?

【问题讨论】:

  • 你试过getComputedStyle()吗? (从您的 mousemove 事件侦听器调用,以便光标实际上是您当时想知道的形状。)
  • 这真的很好用!非常感谢。

标签: javascript cursor icons mouse


【解决方案1】:

一个技巧可能是这样的:

var currentCursor = undefined;
$('*').mouseenter(function(){
    currentCursor = $(this).css('cursor') ;
});

或者不使用JQuery 将是:

var currentCursor = undefined;
addEventListener('mouseenter', function(e) {
    if (e.target instanceof HTMLAnchorElement) {
        currentCursor = e.target.style.cursor;
    }
});

【讨论】:

  • “除非还包括框架/库的另一个标签,否则需要纯 JavaScript 答案。” - 来自当您将鼠标悬停在 info “javascript”标签。 (但如果你觉得必须推荐一个依赖于库的解决方案,至少要提及 which 库。)
猜你喜欢
  • 1970-01-01
  • 2012-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多