简体   繁体   English

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

[英]How can I tell if a browser supports <input type='date'>

Possible Duplicate:可能的重复:
HTML5 Type Detection and Plugin Initialization HTML5 类型检测和插件初始化

<input type=date>

Should create an input with an (optional) user-agent provided datepicker, but its not widely supported yet, so we're using a jQuery UI Datepicker.应该使用(可选)用户代理提供的日期选择器创建输入,但它尚未得到广泛支持,因此我们使用 jQuery UI 日期选择器。 How can we allow browsers to use their own datepicker and fall back on jQuery UI only if the browser doesn't have such a thing?我们如何才能允许浏览器使用自己的日期选择器并仅在浏览器没有这样的东西时才使用 jQuery UI?

At present I think only Opera has a built in datepicker, but a test for Opera would obviously be bad.目前我认为只有 Opera 有内置的日期选择器,但对 Opera 的测试显然很糟糕。 Is there a way this feature can be detected (if it can at all in a portable manner)?有没有办法检测到这个功能(如果它可以以便携的方式)?

The method bellow checks if some input type is supported by most of the browsers:下面的方法检查大多数浏览器是否支持某些输入类型:

function checkInput(type) {
    var input = document.createElement("input");
    input.setAttribute("type", type);
    return input.type == type;
}

But, as simonox mentioned in the comments, some browsers (as Android stock browsers) pretend that they support some type (as date), but they do not offer an UI for date inputs.但是,正如评论中提到的simonox ,一些浏览器(如 Android 股票浏览器)假装它们支持某种类型(如日期),但它们不提供用于日期输入的 UI。 So simonox improved the implementation using the trick of setting an illegal value into the date field.因此, simonox使用在日期字段中设置非法值的技巧改进了实现。 If the browser sanitises this input, it could also offer a datepicker!!!如果浏览器清理了这个输入,它也可以提供一个日期选择器!!!

function checkDateInput() {
    var input = document.createElement('input');
    input.setAttribute('type','date');

    var notADateValue = 'not-a-date';
    input.setAttribute('value', notADateValue); 

    return (input.value !== notADateValue);
}

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

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