简体   繁体   English

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

[英]Can I hide the HTML5 number input’s spin box?

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?跨浏览器是否有一致的方法来隐藏某些浏览器(例如 Chrome)为数字类型的 HTML 输入呈现的新旋转框? I am looking for a CSS or JavaScript method to prevent the up/down arrows from appearing.我正在寻找一种 CSS 或 JavaScript 方法来防止出现向上/向下箭头。

<input id="test" 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)):此 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" />

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.您始终可以使用检查器(webkit,可能是 Firefox 的 Firebug)为您感兴趣的元素查找匹配的 CSS 属性,查找 Pseudo 元素。 This image shows results for an input element type="number":此图显示了输入元素 type="number" 的结果:

输入类型=数字的检查器(Chrome)

Firefox 29 currently adds support for number elements, so here's a snippet for hiding the spinners in webkit and moz based browsers: 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">

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 .在当前版本的 Firefox 中,这些元素的-moz-appearance属性的(用户代理)默认值为number-input Changing that to the value textfield effectively removes the spinner.将其更改为 value textfield有效地删除了微调器。

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). (这是当前 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 .在当前版本的 Chrome 中,这些元素的-webkit-appearance属性的(用户代理)默认值已经是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).为了移除微调器,需要在::-webkit-outer-spin-button / ::-webkit-inner-spin-button伪类上将-webkit-appearance属性的值更改为none (它是-webkit-appearance: inner-spin-button )。

 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.值得指出的是, margin: 0用于删除旧版Chrome 中的边距。

Currently, as of writing this, here is the default user agent styling on the 'inner-spin-button' pseudo class:目前,在撰写本文时,这是“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;
}

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:根据Apple 的移动 Safari 用户体验编码指南,您可以使用以下方法在 iPhone 浏览器中显示数字键盘:

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

A pattern of \d* will also work. \d*pattern也可以工作。

I found a super simple solution using我找到了一个超级简单的解决方案

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

This is supported in most browsers:大多数浏览器都支持此功能:

Try using input type="tel" instead.尝试使用input type="tel"代替。 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.它不需要 JavaScript 或 CSS 或插件或其他任何东西。

Only add this css to remove spinner on input of number仅添加此 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;
}

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 :来自type=number 的 HTML 规范

The type=number state is not appropriate for input that happens to only consist of numbers but isn't strictly speaking a number . type=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.在 GOV.UK 团队进行的可用性研究中发现<input type=number>存在问题。 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.相反,他们建议使用<input type="text" inputmode="numeric" pattern="[0-9]*">这归结为@team_steve 和@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>

I've encountered this problem with a input[type="datetime-local"] , which is similar to this problem.我遇到了input[type="datetime-local"]的这个问题,这与这个问题类似。

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"首先,您必须通过“DevTools -> Settings -> General -> Elements -> Show user agent shadow DOM”打开 chrome 的 shadow-root 功能

Then you can see all shadowed DOM elements , for example, for <input type="number"> , the full element with shadowed DOM is:然后你可以看到所有被遮蔽的 DOM 元素,例如,对于<input type="number"> ,带有被遮蔽 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[type="number"

And according to these info, you can draft some CSS to hide unwanted elements, just as @Josh said.根据这些信息,您可以起草一些 CSS 来隐藏不需要的元素,就像 @Josh 所说的那样。

/* 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 资源

To make this work in Safari I found adding !important to the webkit adjustment forces the spin button to be hidden.为了在 Safari 中进行这项工作,我发现在webkit调整中添加!important会强制隐藏旋转按钮。

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.我仍然无法为 Opera 制定解决方案。

Not what you asked for, but I do this because of a focus bug in WebKit with spinboxes:不是您要求的,但我这样做是因为 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";
}

It might give you an idea to help with what you need.它可能会给你一个想法来帮助你满足你的需要。

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; }

In WebKit and Blink-based browsers & All Kind Of Browser use the following CSS :在 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; 
}

Maybe change the number input with javascript to text input when you don't want a spinner;当您不想要微调器时,也许可以将使用 javascript 的数字输入更改为文本输入;

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;然后让 javascript 将其更改回来,以防您确实需要微调器;

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

it worked well for my purposes它对我的目的很有效

On Firefox for Ubuntu, just using在 Firefox for Ubuntu 上,只需使用

    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'.未知的伪类或伪元素“-webkit-outer-spin-button”。 Ruleset ignored due to bad selector.由于选择器错误,规则集被忽略。

everytime I tried.每次我尝试。 Same for the inner spin button.内部旋转按钮也是如此。

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. appearance: none是关键。

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

For me on chrome nothing has worked but display: none .对我来说,在 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;
}

Credits to @philfreo for pointing it out in the comments.感谢@philfreo 在评论中指出这一点。

This like your css code:这就像你的 CSS 代码:

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

you can use this simple code in your CSS file: 您可以在CSS文件中使用这个简单的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM