我可以隐藏 HTML5 数字输入的旋转框吗?
跨浏览器是否有一致的方法来隐藏某些浏览器(例如 Chrome)为数字类型的 HTML 输入呈现的新旋转框?我正在寻找 CSS 或 JavaScript 方法来防止出现向上/向下箭头。
<input id="test" type="number">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
此 CSS 有效隐藏了 webkit 浏览器的旋转按钮(已在 Chrome 7.0.517.44 和 Safari 版本 5.0.2 (6533.18.5) 中进行了测试):
您始终可以使用检查器(webkit,可能是 Firefox 的 Firebug)来查找您感兴趣的元素的匹配 CSS 属性,查找伪元素。此图片显示输入元素 type="number" 的结果:
This CSS effectively hides the spin-button for webkit browsers (have tested it in Chrome 7.0.517.44 and Safari Version 5.0.2 (6533.18.5)):
You can always use the inspector (webkit, possibly Firebug for Firefox) to look for matched CSS properties for the elements you are interested in, look for Pseudo elements. This image shows results for an input element type="number":
Firefox 29 目前添加了对数字元素的支持,因此这里有一个在基于 webkit 和 moz 的浏览器中隐藏微调器的代码片段:
Firefox 29 currently adds support for number elements, so here's a snippet for hiding the spinners in webkit and moz based browsers:
简短回答:
更长的答案:
要添加到现有答案...
Firefox:
在当前版本的 Firefox 中,
-moz-appearance
属性是number-input
。将其更改为值textfield
可有效删除微调器。在某些情况下,您可能希望微调器最初隐藏,然后在悬停/焦点时显示。 (这是目前 Chrome 中的默认行为)。如果是这样,您可以使用以下方法:
Chrome:
在当前版本的 Chrome 中,
-webkit-appearance
属性 已经是textfield
。为了删除微调器,需要将::-webkit-outer-spin-button< 上的
-webkit-appearance
属性值更改为none
/code>/::-webkit-inner-spin-button
伪类(默认为-webkit-appearance: inside-spin-button
)。值得指出的是,
margin: 0
用于删除旧版 Chrome 中的边距。目前,在撰写本文时,以下是“inner-spin-button”伪类的默认用户代理样式:
Short answer:
Longer answer:
To add to existing answer...
Firefox:
In current versions of Firefox, the (user agent) default value of the
-moz-appearance
property on these elements isnumber-input
. Changing that to the valuetextfield
effectively removes the spinner.In some cases, you may want the spinner to be hidden initially, and then appear on hover/focus. (This is currently the default behavior in Chrome). If so, you can use the following:
Chrome:
In current versions of Chrome, the (user agent) default value of the
-webkit-appearance
property on these elements is alreadytextfield
. In order to remove the spinner, the-webkit-appearance
property's value needs to be changed tonone
on the::-webkit-outer-spin-button
/::-webkit-inner-spin-button
pseudo classes (it is-webkit-appearance: inner-spin-button
by default).It's worth pointing out that
margin: 0
is used to remove the margin in older versions of Chrome.Currently, as of writing this, here is the default user agent styling on the 'inner-spin-button' pseudo class:
根据 Apple 的移动 Safari 用户体验编码指南,您可以使用以下内容在 iPhone 浏览器中显示数字键盘:
\d*
的pattern
将也工作。According to Apple’s user experience coding guide for mobile Safari, you can use the following to display a numeric keyboard in the iPhone browser:
A
pattern
of\d*
will also work.我找到了一个超级简单的解决方案,使用
大多数浏览器都支持此功能:
https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inputmode
https://caniuse.com/input-inputmode< /a>
I found a super simple solution using
This is supported in most browsers:
https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/inputmode
https://caniuse.com/input-inputmode
尝试使用
input type="tel"
代替。它会弹出一个带有数字的键盘,并且不会显示旋转框。它不需要 JavaScript 或 CSS 或插件或其他任何东西。Try using
input type="tel"
instead. It pops up a keyboard with numbers, and it doesn’t show spin boxes. It requires no JavaScript or CSS or plugins or anything else.如果您试图隐藏浏览器添加的控件,则应该暗示您正在滥用该输入类型。
标准一致性问题
type=number 的 HTML 规范中的 :
(重点是我的)
可用性问题
在 一项由 GOV.UK 团队进行的可用性研究。该团队维护着世界上最著名的设计系统之一,并得出结论,该输入类型:
替代解决方案
相反,他们建议使用
,这可以归结为以下组合的答案@team_steve 和@youjin。这不仅隐藏了旋转框,还提高了元素的整体可用性。
不用说,需要进行额外的验证以及有用的错误文本。
If you are trying to hide controls the browser adds, it should be a hint that you are misusing that input type.
Standard Conformance Issues
From the HTML spec on type=number:
(Emphasis is mine)
Usability Issues
The
<input type=number>
was found to be problematic during a usability study performed by the GOV.UK team. This team maintains one of the world's best known design systems and concludes that this input type:Alternative Solution
Instead, they recommend to use
<input type="text" inputmode="numeric" pattern="[0-9]*">
which comes down to the combined answers of @team_steve and @youjin. This not only hides the spin boxes, it also improves the overall usability of the element.It comes without saying that additional validation needs to be done, along with a helpful error text.
仅添加此 css 即可删除输入数字时的微调器
Only add this css to remove spinner on input of number
来源
Source
我在
input[type="datetime-local"]
中遇到了这个问题,与这个问题类似。我找到了克服此类问题的方法。
首先,你必须通过“DevTools -> Settings -> General -> Elements -> Show user agent Shadow DOM”打开chrome的shadow-root功能,
然后你就可以看到所有shadowed DOM元素 ,例如,对于
,具有阴影 DOM 的完整元素为:
根据这些信息,您可以起草一些CSS来隐藏不需要的元素,就像@Josh所说。
I've encountered this problem with a
input[type="datetime-local"]
, which is similar to this problem.And I've found a way to overcome this kind of problems.
First, you must turn on chrome's shadow-root feature by "DevTools -> Settings -> General -> Elements -> Show user agent shadow DOM"
Then you can see all shadowed DOM elements, for example, for
<input type="number">
, the full element with shadowed DOM is:And according to these info, you can draft some CSS to hide unwanted elements, just as @Josh said.
为了在 Safari 中实现此功能,我发现将
!important
添加到 webkit 调整中会强制隐藏旋转按钮。我仍然无法找到 Opera 的解决方案。
To make this work in Safari I found adding
!important
to the webkit adjustment forces the spin button to be hidden.I am still having trouble working out a solution for Opera as well.
当您不需要旋转器时,可以将使用 javascript 的数字输入更改为文本输入;
并阻止用户输入文本;
然后让 javascript 将其更改回来,以防万一您确实需要旋转器;
它很适合我的目的
Maybe change the number input with javascript to text input when you don't want a spinner;
and stop the user entering text;
then have the javascript change it back in case you do want a spinner;
it worked well for my purposes
这是更好的答案,我想建议鼠标悬停和不悬停
This is more better answer i would like to suggest on mouse over and without mouse over
不是您所要求的,但我这样做是因为 WebKit 中带有旋转框的焦点错误:
它可能会给您一个想法来帮助您满足您的需求。
Not what you asked for, but I do this because of a focus bug in WebKit with spinboxes:
It might give you an idea to help with what you need.
在 Firefox for Ubuntu 上,仅仅使用
就对我有用了。
添加
会导致我
每次我尝试。内部旋转按钮也是如此。
On Firefox for Ubuntu, just using
did the trick for me.
Adding
Would lead me to
everytime I tried. Same for the inner spin button.
我需要这个来工作
额外的一行
appearance: none
对我来说很关键。I needed this to work
The extra line of
appearance: none
was key to me.在基于 WebKit 和 Blink 的浏览器中所有类型的浏览器都使用以下 CSS :
In WebKit and Blink-based browsers & All Kind Of Browser use the following CSS :
对于我来说,在 chrome 上除了
display: none
之外什么都不起作用。感谢 @philfreo 在评论中指出这一点。
For me on chrome nothing has worked but
display: none
.Credits to @philfreo for pointing it out in the comments.
这就像你的 CSS 代码:
This like your css code: