最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Add javascript fallback for input type="date" where browser support is not available - Stack Overflow

programmeradmin1浏览0评论

Adding type date to input fields now produces a browser based date picker. (Where supported).

<input type="date"></input>

This is fantasic for touch devies however...

When browsing with the meat of the market: firefox and internet explorer, type date is not supported.


QUESTION

How to use input type="date" and fallback to a javascript date picker when support is not available?

Currently I cant seem to get the best of both worlds without producing both date pickers simultaneously.

Adding type date to input fields now produces a browser based date picker. (Where supported).

<input type="date"></input>

This is fantasic for touch devies however...

When browsing with the meat of the market: firefox and internet explorer, type date is not supported.


QUESTION

How to use input type="date" and fallback to a javascript date picker when support is not available?

Currently I cant seem to get the best of both worlds without producing both date pickers simultaneously.

Share Improve this question edited Jul 27, 2015 at 7:48 DreamTeK asked Jan 24, 2014 at 10:29 DreamTeKDreamTeK 34.2k29 gold badges122 silver badges178 bronze badges 1
  • Browsers that don’t support type=date should report the type as the default text when it’s queried via JS … – C3roe Commented Jan 24, 2014 at 10:37
Add a comment  | 

2 Answers 2

Reset to default 13

You should look into using modernizr which uses JS to work out what features the current browser has. In the below example you can serve another datepicker if this browser isn't compatible:

<script src="modernizr.js"></script>
<script>Modernizr.load({
  test: Modernizr.inputtypes.date,
  nope: ['http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js', 'jquery-ui.css'],
  complete: function () {
    $('input[type=date]').datepicker({
      dateFormat: 'yy-mm-dd'
    }); 
  }
});
</script>

Have a look at this polyfill :

https://github.com/chemerisuk/better-dateinput-polyfill

发布评论

评论列表(0)

  1. 暂无评论