最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - html5 input type number: detect whether spinbox or not (and no other features) - Stack Overflow

programmeradmin2浏览0评论

In Webkit browsers, an input[type=number] has a spinbox control:

However, Safari does not follow some other input[type=number] rules, like enforcing that only numeric characters are entered. Hence, Modernizr detects that Safari does not support input[type=number].

I have very particular needs for number-input width, and when there's a spinbox I make the width 2.7em and without it (like in Firefox), the width need only be 1.7em. So Chrome and Firefox both look fine. But Safari puts in a spinbox but doesn't follow any other rules, so it gets the 1.7em width and looks like this:

I only care if there's a spinbox control. I don't care about any of the other input[type=number] rules that Safari is flouting. Safari is playing by the only rule I care about. How do I detect that?

In Webkit browsers, an input[type=number] has a spinbox control:

However, Safari does not follow some other input[type=number] rules, like enforcing that only numeric characters are entered. Hence, Modernizr detects that Safari does not support input[type=number].

I have very particular needs for number-input width, and when there's a spinbox I make the width 2.7em and without it (like in Firefox), the width need only be 1.7em. So Chrome and Firefox both look fine. But Safari puts in a spinbox but doesn't follow any other rules, so it gets the 1.7em width and looks like this:

I only care if there's a spinbox control. I don't care about any of the other input[type=number] rules that Safari is flouting. Safari is playing by the only rule I care about. How do I detect that?

Share Improve this question edited Feb 8, 2017 at 14:31 CommunityBot 11 silver badge asked Mar 19, 2011 at 16:18 chadohchadoh 4,4326 gold badges42 silver badges65 bronze badges 1
  • 1 This is just a guess, but could you try determining the width of the input, change it to type "number", remeasure the width and see if it has changed? I doubt that's gonna work, but it's an idea. – Tower Commented Apr 17, 2011 at 15:33
Add a ment  | 

2 Answers 2

Reset to default 7 +50

I suggest hiding the spinbox when Modernizr doesn't detect support for number inputs:

JS:

if (!Modernizr.inputtypes.number) {
    $('body').addClass('no-number-input');
}

CSS:

.no-number-input input::-webkit-outer-spin-button,
.no-number-input input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0; 
}

If you really want to detect whether the spinbox is visible you can do something like:

window.getComputedStyle(input, '-webkit-inner-spin-button').getPropertyValue('-webkit-appearance') != 'none'

where input is a "raw" element, although this may not be very reliable, e.g. in Safari for Windows.

if (Modernizr.inputtypes.number) {
  $('input[type=number]').width('2.7em');
} else {
  $('input[type=number]').width('1.7em');
}
发布评论

评论列表(0)

  1. 暂无评论