简体   繁体   English

html5的用法<input type="month">针对不同的浏览器

[英]Usage of html5 <input type="month"> for different browsers

I would like to create an input field which lets the user select a month and a year.我想创建一个输入字段,让用户 select 一个月和一年。 HTML5 introduced the input type "month" which suits my problem. HTML5 介绍了适合我的问题的输入类型“月”。 It works fine in Chrome, Opera and Edge ( https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/month ).它在 Chrome、Opera 和 Edge 中运行良好( https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/month )。

If the user is using Firefox (or IE or any older Browser version), the input field doesn't show a date picker but instead expects a plain text input.如果用户使用 Firefox(或 IE 或任何旧版浏览器),输入字段不会显示日期选择器,而是需要纯文本输入。

How can I implement my form in a way, that if the current browser supports input type="month", it shows the native browser datepicker, and if that's not the case, it shows a custom date picker?我如何以某种方式实现我的表单,如果当前浏览器支持 input type="month",它会显示本机浏览器日期选择器,如果不是这种情况,它会显示自定义日期选择器? I expect the input type="month" to work in future Firefox versions and would like to automaticly use that functionality once it exists.我希望 input type="month" 在未来的 Firefox 版本中工作,并且希望在该功能存在后自动使用它。

What would be the best way to implement a custom date picker?实现自定义日期选择器的最佳方式是什么?

I'm already using JQuery in my project.我已经在我的项目中使用 JQuery。 Furthermore I'm using Laravel, although I don't think that Laravel offers any utily in that regard.此外,我正在使用 Laravel,尽管我认为 Laravel 在这方面没有任何用处。

Yes HTML5 elements are not supported on the old browsers. 是,旧的浏览器不支持HTML5元素。 You can't expect new technologies to work on old systems. 您不能指望新技术可以在旧系统上运行。 :) :)

So as you already know you can use a custom date picker. 因此,您已经知道可以使用自定义日期选择器。 And I will recommend http://www.daterangepicker.com/ , as I have been using it for some time now.Pretty stable. 我会推荐http://www.daterangepicker.com/ ,因为我已经使用了一段时间了。非常稳定。

Not recommending jQueryUI datepicker anymore, as it come with lot of other stuff as a bulk and it will slowdown your app loading time. 不再建议使用jQueryUI datepicker,因为它附带了很多其他东西,这会减慢您的应用程序加载时间。

Give shot to daterangepicker it will solve your issue. 将镜头交给daterangepicker,它将解决您的问题。 ;) ;)

In browsers that don't support month inputs, the control degrades gracefully to a simple <input type="text">在不支持月份输入的浏览器中,控件优雅地降级为简单的<input type="text">

You can iterate over all the input elements with [type=month] and check what their actual type property says:您可以使用[type=month]迭代所有输入元素并检查它们的实际type属性表示什么:

document.querySelectorAll(`input[type=month]`).forEach((inp) => {
  if (inp.type === 'text') {
    // whatever, e.g. initialize the custom datepicker
  }
});

是否可以显示您指定的日历( http://www.daterangepicker.com/, ),只显示月份和年份,而不显示日期?

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

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