如何判断浏览器是否支持<input type ='date'>

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)

  • 要求type属性不适用于所有Android股票浏览器.他们假装htat他们支持inputType = date,但他们没有提供日期输入的UI.这个特征检测对我有用:function(){var el = document.createElement('input'),notADateValue ='not-a-date'; el.setAttribute( '类型', '日期'); el.setAttribute('value',notADateValue); return!(el.value === notADateValue); 诀窍是将非法值设置为日期字段.如果浏览器清理此输入,它还可以提供日期选择器. (32认同)
  • 在android股票浏览器中不起作用,它返回true但不支持datepicker (8认同)
  • 你的意思是'!==='='!=='?:) (3认同)
  • 这是一招吗?是.它有用吗?是. (2认同)