我如何知道浏览器是否支持< input type ='date'> [英] How can I tell if a browser supports <input type='date'>

查看:573
本文介绍了我如何知道浏览器是否支持< input type ='date'>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

< b $ b

 < input type = date> 

应该用一个(可选的)用户代理提供的datepicker来创建一个输入,但是它还没有得到广泛的支持,所以我们使用了一个jQuery UI Datepicker。我们如何才能允许浏览器使用自己的日期选择器,只有当浏览器没有这样的东西时才会回到jQuery UI?



目前我认为只有Opera有一个内置日期选择器,但对Opera的测试显然是不好的。有没有一种方法可以检测到这个功能(如果它可以根本以便携的方式)?

谢谢



  function checkInput(type){
var input = document.createElement(input);
input.setAttribute(type,type);
返回input.type ==类型;
}

但是,如 simonox ,一些浏览器(如Android股票浏览器)假装它们支持某种类型(如日期),但它们不提供日期输入的UI。所以 simonox 使用在日期字段中设置非法值的技巧改进了实现。如果浏览器清理了这个输入,它也可以提供一个日期选择器!!!

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

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

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


Possible Duplicate:
HTML5 Type Detection and Plugin Initialization

<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. 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?

At present I think only Opera has a built in datepicker, but a test for Opera would obviously be bad. Is there a way this feature can be detected (if it can at all in a portable manner)?

Thanks

解决方案

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. So simonox improved the implementation using the trick of setting an illegal value into the date field. 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);
}

这篇关于我如何知道浏览器是否支持&lt; input type ='date'&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆