【问题标题】:Mouse event cursor difference between browsers浏览器之间的鼠标事件光标差异
【发布时间】:2016-08-19 23:13:53
【问题描述】:

我使用鼠标悬停来获取光标类型,当鼠标悬停时,我在 Chrome 和 Firefox 中得到不同的结果,由于某种原因,Chrome 中的光标样式是“自动”,而在 Firefox 中是“文本”。我需要知道光标在两个浏览器中什么时候是默认的,什么时候是自动的(因为它假设是在文本输入之上)或文本。 .

我在这里写了一个简单的代码来重现这个问题,在 Chrome 和 Firefox 上尝试一下,看看有什么不同(如果你想玩代码,这里是jsfiddle)。

提前谢谢:)

window.onmouseover=function(event) {
    var currentCursor = $(event.target).css('cursor'); 
    console.log(currentCursor);
    $('#pointer').html(currentCursor);
};
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<input type="text" width="100">
<p>Move the mouse in and out the input field</p>
<p id='pointer'></p>

【问题讨论】:

  • CSS 可以将其更改为您想要的任何内容。为什么需要知道光标类型?
  • 这是我正在构建的一个功能,如果根据光标样式发生变化,我不需要更改光标。
  • 那么没有光标的用户如何使用这个功能呢?
  • 它只针对使用鼠标的桌面用户,它是我正在构建的 WordPress 插件中的一个功能。
  • 您提到它应该是 auto 在文本字段上。您是否在某处找到了该规范?

标签: javascript jquery


【解决方案1】:

Chrome 和 Firefox 可能对输入控件使用不同的默认表单。

如果您想确保可以设置自己的样式表并强制光标为文本类型输入的“文本”。

window.onmouseover=function(event) {
    var currentCursor = $(event.target).css('cursor'); 
    console.log(currentCursor);
    $('#pointer').html(currentCursor);
};
input[type="text"] {
  cursor:text
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<input type="text" width="100">
<p>Move the mouse in and out the input field</p>
<p id='pointer'></p>

【讨论】:

  • 这太棒了,我在 js 中看着如此黑暗的地方,以至于我没有想到 css。谢谢
猜你喜欢
  • 1970-01-01
  • 2010-11-24
  • 1970-01-01
  • 1970-01-01
  • 2011-11-28
  • 2013-02-02
  • 2018-07-16
  • 2012-05-09
相关资源
最近更新 更多