Input type Range showing textbox instead of slider
Here is my code:
<input type="range" name="slider" id="slider" value="5开发者_开发问答" min="0" max="10" step="1" />
All HTML5 mobile browsers I have tested this in (iPhone 4, Android, iPad) render this as input type=text. Why won't this render a slider?
Not all browsers support it yet. You can always use http://www.modernizr.com/ to test if the browser supports it and then use http://jqueryui.com/ as a fallback. Try something like this.
var initSlider = function() {
$('input[type=range]').each(function() {
var $input = $(this);
var $slider = $('<div id="' + $input.attr('id') + '" class="' + $input.attr('class') + '"></div>');
var step = $input.attr('step');
$input.after($slider).hide();
$slider.slider({
min: $input.attr('min'),
max: $input.attr('max'),
step: $input.attr('step'),
change: function(e, ui) {
$(this).val(ui.value);
}
});
});
};
According to this post it's not implemented in iOS 4.2 and not fully implemented in Android 2.3 (whatever that means). I did test on iOS 4.3 and it looks like it's still not implemented for mobile safari.
<input type="number"
does not seem to work either.
Support for input type="range" has since been added within iOS 5, so you will get the slider in iOS 5, but not before.
Based on Modernizr, the best way to test for input type="range" compatibility would be:
var i = document.createElement("input"), safe = false;
i.setAttribute("type", "range");
if(i.type !== "text"){
i.value = ':)';
i.style.cssText = 'position:absolute;visibility:hidden;';
if (i.style.WebkitAppearance !== undefined ) {
document.documentElement.appendChild(i);
defaultView = document.defaultView;
safe = defaultView.getComputedStyle &&
defaultView.getComputedStyle(i, null).WebkitAppearance !== 'textfield' &&
(i.offsetHeight !== 0);
document.documentElement.removeChild(i);
}
}
return safe? '<input type="range" />': '<input type="number" />';
Hope it helps.
精彩评论