简体   繁体   English

HTML5号字段箭头自定义

[英]HTML5 number field arrows customization

Can I customize 我可以自定义

<input type='number'> 

field to show all the time it's arrows? 字段一直显示它的箭头? By default it's hidden till the field is has no focus. 默认情况下它是隐藏的,直到该字段没有焦点。 Below is what I'm talking about. 以下就是我所说的。

在此输入图像描述

Firefox and IE don't have such behavior. Firefox和IE没有这种行为。 So, I assume you are working with Google Chrome. 因此,我假设您正在使用Google Chrome。

input::-webkit-inner-spin-button {
    opacity: 1;
}

FYI. 仅供参考。 UA stylesheet has the following: UA样式表具有以下内容:

input::-webkit-inner-spin-button {
    ...
    opacity: 0;
    pointer-events: none;
}

input:enabled:read-write:-webkit-any(:focus,:hover)::-webkit-inner-spin-button {
    opacity: 1;
    pointer-events: auto;
}

html.css html.css

The UI and behavior of <input type='number'> , as well as all the other HTML5 input types (eg, type='date' , etc), is browser and/or system dependent. <input type='number'>的UI和行为,以及所有其他HTML5输入类型(例如, type='date'等),取决于浏览器和/或系统。 To make the arrows always visible, you'd need to use a custom JS solution. 要使箭头始终可见,您需要使用自定义JS解决方案。

Only way that I can think of is... Having two buttons for incrementing and decrementing your input and using JS. 我能想到的唯一方法是......有两个按钮用于递增和递减input并使用JS。 You won't be using type="number" here since the JS will be incrementing and decrementing the number for you. 你不会在这里使用type="number" ,因为JS会为你增加和减少数字。

Here is an example, as mentioned here : 下面是一个例子,提到这里

CSS: CSS:

.spin {
    display: inline-block;
}
.spin span {
    display: inline-block;
    width: 20px;
    height: 22px;
    text-align: center;
    padding-top: 2px;
    background: #fff;
    border: 1px solid #aaa;
    border-radius: 0 4px 4px 0;
    cursor: pointer;
}
.spin span:first-child {
    border-radius: 4px 0 0 4px;
}
.spin input {
    width: 40px;
    height: 20px;
    text-align: center;
    font-weight: bold;
}

JS: JS:

var spins = document.getElementsByClassName("spin");
for (var i = 0, len = spins.length; i < len; i++) {
    var spin = spins[i],
        span = spin.getElementsByTagName("span"),
        input = spin.getElementsByTagName("input")[0];

    input.onchange = function() { input.value = +input.value || 0; };
    span[0].onclick = function() { input.value = Math.max(0, input.value - 1); };
    span[1].onclick = function() { input.value -= -1; };
}

Note: Change background: #fff; 注意:更改background: #fff; to change the arrow colors. 改变箭头颜色。 There are other neat examples available on the web as well! 网上还有其他简洁的例子!

Demo 演示

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

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