Dae*_*all 77 html javascript html5
可能重复:
HTML5类型检测和插件初始化
<input type=date>
Run Code Online (Sandbox Code Playgroud)
应该使用(可选的)用户代理提供的datepicker创建一个输入,但它还没有被广泛支持,所以我们使用的是jQuery UI Datepicker.我们怎样才能允许浏览器使用自己的日期选择器,并且只有在浏览器没有这样的东西时才能回退到jQuery UI?
目前我认为只有Opera有一个内置的日期选择器,但Opera的测试显然是不好的.有没有办法检测到这个功能(如果它可以以便携方式完成)?
Ita*_*tto 125
下面的方法检查大多数浏览器是否支持某些输入类型:
function checkInput(type) {
var input = document.createElement("input");
input.setAttribute("type", type);
return input.type == type;
}
Run Code Online (Sandbox Code Playgroud)
但是,正如评论中提到的simonox,某些浏览器(如Android股票浏览器)假装它们支持某种类型(作为日期),但它们不提供日期输入的UI.所以simonox使用在日期字段中设置非法值的技巧改进了实现.如果浏览器清理此输入,它还可以提供日期选择器!
function checkDateInput() {
var input = document.createElement('input');
input.setAttribute('type','date');
var notADateValue = 'not-a-date';
input.setAttribute('value', notADateValue);
return (input.value !== notADateValue);
}
Run Code Online (Sandbox Code Playgroud)