我可以隐藏 HTML5 数字输入的旋转框吗?

发布于 2024-09-25 00:23:01 字数 236 浏览 5 评论 0 原文

跨浏览器是否有一致的方法来隐藏某些浏览器(例如 Chrome)为数字类型的 HTML 输入呈现的新旋转框?我正在寻找 CSS 或 JavaScript 方法来防止出现向上/向下箭头。

<input id="test" type="number">

Is there a consistent way across browsers to hide the new spin boxes that some browsers (such as Chrome) render for HTML input of type number? I am looking for a CSS or JavaScript method to prevent the up/down arrows from appearing.

<input id="test" type="number">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

守不住的情 2024-10-02 00:23:01

此 CSS 有效隐藏了 webkit 浏览器的旋转按钮(已在 Chrome 7.0.517.44 和 Safari 版本 5.0.2 (6533.18.5) 中进行了测试):

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    /* display: none; <- Crashes Chrome on hover */
    -webkit-appearance: none;
    margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
}

input[type=number] {
    -moz-appearance:textfield; /* Firefox */
}
<input type="number" step="0.01" />

您始终可以使用检查器(webkit,可能是 Firefox 的 Firebug)来查找您感兴趣的元素的匹配 CSS 属性,查找伪元素。此图片显示输入元素 type="number" 的结果:

Inspector for input type=number (Chrome)

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)):

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    /* display: none; <- Crashes Chrome on hover */
    -webkit-appearance: none;
    margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
}

input[type=number] {
    -moz-appearance:textfield; /* Firefox */
}
<input type="number" step="0.01" />

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":

Inspector for input type=number (Chrome)

燃情 2024-10-02 00:23:01

Firefox 29 目前添加了对数字元素的支持,因此这里有一个在基于 webkit 和 moz 的浏览器中隐藏微调器的代码片段:

input[type='number'] {
    -moz-appearance:textfield;
}

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none;
}
<input id="test" type="number">

Firefox 29 currently adds support for number elements, so here's a snippet for hiding the spinners in webkit and moz based browsers:

input[type='number'] {
    -moz-appearance:textfield;
}

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none;
}
<input id="test" type="number">

像极了他 2024-10-02 00:23:01

简短回答:

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
input[type="number"] {
    -moz-appearance: textfield;
}
<input type="number" />

更长的答案:

要添加到现有答案...

Firefox:

在当前版本的 Firefox 中,-moz-appearance 属性number-input。将其更改为值 textfield 可有效删除微调器。

input[type="number"] {
    -moz-appearance: textfield;
}

在某些情况下,您可能希望微调器最初隐藏,然后在悬停/焦点时显示。 (这是目前 Chrome 中的默认行为)。如果是这样,您可以使用以下方法:

input[type="number"] {
    -moz-appearance: textfield;
}
input[type="number"]:hover,
input[type="number"]:focus {
    -moz-appearance: number-input;
}
<input type="number"/>


Chrome:

在当前版本的 Chrome 中,-webkit-appearance 属性 已经是 textfield。为了删除微调器,需要将 ::-webkit-outer-spin-button< 上的 -webkit-appearance 属性值更改为 none /code>/::-webkit-inner-spin-button 伪类(默认为 -webkit-appearance: inside-spin-button)。

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
<input type="number" />

值得指出的是,margin: 0 用于删除旧版 Chrome 中的边距。

目前,在撰写本文时,以下是“inner-spin-button”伪类的默认用户代理样式:

input::-webkit-inner-spin-button {
    -webkit-appearance: inner-spin-button;
    display: inline-block;
    cursor: default;
    flex: 0 0 auto;
    align-self: stretch;
    -webkit-user-select: none;
    opacity: 0;
    pointer-events: none;
    -webkit-user-modify: read-only;
}

Short answer:

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
input[type="number"] {
    -moz-appearance: textfield;
}
<input type="number" />

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 is number-input. Changing that to the value textfield effectively removes the spinner.

input[type="number"] {
    -moz-appearance: textfield;
}

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:

input[type="number"] {
    -moz-appearance: textfield;
}
input[type="number"]:hover,
input[type="number"]:focus {
    -moz-appearance: number-input;
}
<input type="number"/>


Chrome:

In current versions of Chrome, the (user agent) default value of the -webkit-appearance property on these elements is already textfield. In order to remove the spinner, the -webkit-appearance property's value needs to be changed to none on the ::-webkit-outer-spin-button/::-webkit-inner-spin-button pseudo classes (it is -webkit-appearance: inner-spin-button by default).

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
<input type="number" />

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:

input::-webkit-inner-spin-button {
    -webkit-appearance: inner-spin-button;
    display: inline-block;
    cursor: default;
    flex: 0 0 auto;
    align-self: stretch;
    -webkit-user-select: none;
    opacity: 0;
    pointer-events: none;
    -webkit-user-modify: read-only;
}
謌踐踏愛綪 2024-10-02 00:23:01

根据 Apple 的移动 Safari 用户体验编码指南,您可以使用以下内容在 iPhone 浏览器中显示数字键盘:

<input type="text" pattern="[0-9]*" />

\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:

<input type="text" pattern="[0-9]*" />

A pattern of \d* will also work.

柠栀 2024-10-02 00:23:01

我找到了一个超级简单的解决方案,使用

<input type="text" inputmode="numeric" />

大多数浏览器都支持此功能:

I found a super simple solution using

<input type="text" inputmode="numeric" />

This is supported in most browsers:

指尖上得阳光 2024-10-02 00:23:01

尝试使用 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.

ぃ双果 2024-10-02 00:23:01

如果您试图隐藏浏览器添加的控件,则应该暗示您正在滥用该输入类型。

标准一致性问题

type=number 的 HTML 规范中的

type=number 状态不适合仅包含数字但严格来说不是数字的输入。例如,它不适用于信用卡号或美国邮政编码。

(重点是我的)

可用性问题

一项由 GOV.UK 团队进行的可用性研究。该团队维护着世界上最著名的设计系统之一,并得出结论,该输入类型:

替代解决方案

相反,他们建议使用 ,这可以归结为以下组合的答案@team_steve 和@youjin。这不仅隐藏了旋转框,还提高了元素的整体可用性。

不用说,需要进行额外的验证以及有用的错误文本。

<label>Postal Code
<input type="text" inputmode="numeric" pattern="[0-9]*" size="5">
</label>

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:

The type=number state is not appropriate for input that happens to only consist of numbers but isn't strictly speaking a number. For example, it would be inappropriate for credit card numbers or US postal codes.

(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.

<label>Postal Code
<input type="text" inputmode="numeric" pattern="[0-9]*" size="5">
</label>

红衣飘飘貌似仙 2024-10-02 00:23:01

仅添加此 css 即可删除输入数字时的微调器

/* For Firefox */

input[type='number'] {
    -moz-appearance:textfield;
}



/* Webkit browsers like Safari and Chrome */

input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

Only add this css to remove spinner on input of number

/* For Firefox */

input[type='number'] {
    -moz-appearance:textfield;
}



/* Webkit browsers like Safari and Chrome */

input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
去了角落 2024-10-02 00:23:01
/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Firefox */
input[type=number] {
  -moz-appearance: textfield;
}

来源

/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Firefox */
input[type=number] {
  -moz-appearance: textfield;
}

Source

蓝海似她心 2024-10-02 00:23:01

我在 input[type="datetime-local"] 中遇到了这个问题,与这个问题类似。

我找到了克服此类问题的方法。

首先,你必须通过“DevTools -> Settings -> General -> Elements -> Show user agent Shadow DOM”打开chrome的shadow-root功能,

然后你就可以看到所有shadowed DOM元素 ,例如,对于 ,具有阴影 DOM 的完整元素为:

<input type="number">
  <div id="text-field-container" pseudo="-webkit-textfield-decoration-container">
    <div id="editing-view-port">
      <div id="inner-editor"></div>
    </div>
    <div pseudo="-webkit-inner-spin-button" id="spin"></div>
  </div>
</input>

影子 DOM input[type=

根据这些信息,您可以起草一些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:

<input type="number">
  <div id="text-field-container" pseudo="-webkit-textfield-decoration-container">
    <div id="editing-view-port">
      <div id="inner-editor"></div>
    </div>
    <div pseudo="-webkit-inner-spin-button" id="spin"></div>
  </div>
</input>

shadow DOM of input[type="number"

And according to these info, you can draft some CSS to hide unwanted elements, just as @Josh said.

哆兒滾 2024-10-02 00:23:01

为了在 Safari 中实现此功能,我发现将 !important 添加到 webkit 调整中会强制隐藏旋转按钮。

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    /* display: none; <- Crashes Chrome on hover */
    -webkit-appearance: none !important;
    margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
}

我仍然无法找到 Opera 的解决方案。

To make this work in Safari I found adding !important to the webkit adjustment forces the spin button to be hidden.

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    /* display: none; <- Crashes Chrome on hover */
    -webkit-appearance: none !important;
    margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
}

I am still having trouble working out a solution for Opera as well.

凉风有信 2024-10-02 00:23:01

当您不需要旋转器时,可以将使用 javascript 的数字输入更改为文本输入;

document.getElementById('myinput').type = 'text';

并阻止用户输入文本;

  document.getElementById('myinput').onkeydown = function(e) {
  if(!((e.keyCode > 95 && e.keyCode < 106)
    || (e.keyCode > 47 && e.keyCode < 58) 
    || e.keyCode == 8
    || e.keyCode == 9)) {
          return false;
      }
  }

然后让 javascript 将其更改回来,以防万一您确实需要旋转器;

document.getElementById('myinput').type = 'number';

它很适合我的目的

Maybe change the number input with javascript to text input when you don't want a spinner;

document.getElementById('myinput').type = 'text';

and stop the user entering text;

  document.getElementById('myinput').onkeydown = function(e) {
  if(!((e.keyCode > 95 && e.keyCode < 106)
    || (e.keyCode > 47 && e.keyCode < 58) 
    || e.keyCode == 8
    || e.keyCode == 9)) {
          return false;
      }
  }

then have the javascript change it back in case you do want a spinner;

document.getElementById('myinput').type = 'number';

it worked well for my purposes

命比纸薄 2024-10-02 00:23:01

这是更好的答案,我想建议鼠标悬停和不悬停

input[type='number'] {
  appearance: textfield;
}
input[type='number']::-webkit-inner-spin-button,
input[type='number']::-webkit-outer-spin-button,
input[type='number']:hover::-webkit-inner-spin-button, 
input[type='number']:hover::-webkit-outer-spin-button {
-webkit-appearance: none; 
 margin: 0; }

This is more better answer i would like to suggest on mouse over and without mouse over

input[type='number'] {
  appearance: textfield;
}
input[type='number']::-webkit-inner-spin-button,
input[type='number']::-webkit-outer-spin-button,
input[type='number']:hover::-webkit-inner-spin-button, 
input[type='number']:hover::-webkit-outer-spin-button {
-webkit-appearance: none; 
 margin: 0; }
谈下烟灰 2024-10-02 00:23:01

不是您所要求的,但我这样做是因为 WebKit 中带有旋转框的焦点错误:

// temporary fix for focus bug with webkit input type=number ui
if (navigator.userAgent.indexOf("AppleWebKit") > -1 && navigator.userAgent.indexOf("Mobile") == -1)
{
    var els = document.querySelectorAll("input[type=number]");
    for (var el in els)
        el.type = "text";
}

它可能会给您一个想法来帮助您满足您的需求。

Not what you asked for, but I do this because of a focus bug in WebKit with spinboxes:

// temporary fix for focus bug with webkit input type=number ui
if (navigator.userAgent.indexOf("AppleWebKit") > -1 && navigator.userAgent.indexOf("Mobile") == -1)
{
    var els = document.querySelectorAll("input[type=number]");
    for (var el in els)
        el.type = "text";
}

It might give you an idea to help with what you need.

夏末染殇 2024-10-02 00:23:01

在 Firefox for Ubuntu 上,仅仅使用

    input[type='number'] {
    -moz-appearance:textfield;
}

就对我有用了。

添加

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none;
}

会导致我

未知的伪类或伪元素“-webkit-outer-spin-button”。
由于选择器错误而忽略了规则集。

每次我尝试。内部旋转按钮也是如此。

On Firefox for Ubuntu, just using

    input[type='number'] {
    -moz-appearance:textfield;
}

did the trick for me.

Adding

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none;
}

Would lead me to

Unknown pseudo-class or pseudo-element ‘-webkit-outer-spin-button’.
Ruleset ignored due to bad selector.

everytime I tried. Same for the inner spin button.

你怎么敢 2024-10-02 00:23:01

我需要这个来工作

/* Chrome, Safari, Edge, Opera */
input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button
  -webkit-appearance: none
  appearance: none
  margin: 0

/* Firefox */
input[type=number]
  -moz-appearance: textfield

额外的一行 appearance: none 对我来说很关键。

I needed this to work

/* Chrome, Safari, Edge, Opera */
input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button
  -webkit-appearance: none
  appearance: none
  margin: 0

/* Firefox */
input[type=number]
  -moz-appearance: textfield

The extra line of appearance: none was key to me.

半山落雨半山空 2024-10-02 00:23:01

在基于 WebKit 和 Blink 的浏览器中所有类型的浏览器都使用以下 CSS :

/* Disable Number Arrow */
input[type=number]::-webkit-inner-spin-button, 
input[type=number]::-webkit-outer-spin-button { 
  -webkit-appearance: none; 
  margin: 0; 
}

In WebKit and Blink-based browsers & All Kind Of Browser use the following CSS :

/* Disable Number Arrow */
input[type=number]::-webkit-inner-spin-button, 
input[type=number]::-webkit-outer-spin-button { 
  -webkit-appearance: none; 
  margin: 0; 
}
妥活 2024-10-02 00:23:01

对于我来说,在 chrome 上除了 display: none 之外什么都不起作用。

/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
  display: none;
}

/* Firefox */
input[type=number] {
  -moz-appearance: textfield;
}

感谢 @philfreo 在评论中指出这一点。

For me on chrome nothing has worked but display: none.

/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
  display: none;
}

/* Firefox */
input[type=number] {
  -moz-appearance: textfield;
}

Credits to @philfreo for pointing it out in the comments.

我早已燃尽 2024-10-02 00:23:01
input[type=number]::-webkit-inner-spin-button, 
input[type=number]::-webkit-outer-spin-button {
     -webkit-appearance: none;
<input id="test" type="number">

input[type=number]::-webkit-inner-spin-button, 
input[type=number]::-webkit-outer-spin-button {
     -webkit-appearance: none;
<input id="test" type="number">

ゞ记忆︶ㄣ 2024-10-02 00:23:01

这就像你的 CSS 代码:

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

This like your css code:

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文