通过 jQuery 检测对特定 HTML 5 功能的支持
我正在编写一些 HTML5 演示代码,包括诸如 之类的内容,
目前这在 Opera 10 中可以正常工作,但其他浏览器只显示正常的文本输入。然后,我使用 jQuery.date-input 插件在不支持它的浏览器上覆盖此行为。
问题是 - jQuery 也在 Opera 上运行,所以在 Opera 中我得到了两个日历日期选择器(一个来自浏览器,一个来自 jQuery)
我现在可以使用 if (window.opera) 解决这个问题
- 但是有什么方法可以使用 jQuery.support 等方法可靠地检测当前浏览器是否支持特定的 HTML5 功能?
I'm working on some HTML5 demo code, including stuff like <input type="date" />
This currently works correctly in Opera 10 as-is, but every other browser just displays a normal text input. I'm then using a jQuery.date-input plugin to override this behaviour on browsers that don't support it.
Problem is - the jQuery's running on Opera as well, so in Opera I'm getting two calendar date-pickers (one from the browser, one from jQuery)
I can work around this for now using if (window.opera)
- but is there some way using, say, jQuery.support, that I can reliably detect whether the current browser supports a particular HTML5 feature or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会看一下 Modernizr。它是一个开源的、获得 MIT 许可的 Javascript 库,可以检测对许多 HTML5/CSS3 功能的支持,而且它非常小(压缩后为 7kb)。要使用它,只需:
在文档的
中。之后,它有多种功能来检查您需要的内容。示例:
还有很多方法可以检查您想要的特定 HTML5/CSS3 功能。 查看他们的网站以获取它并查看文档。
I would take a look at Modernizr. It's an open source, MIT-licensed Javascript library that detects support for many HTML5/CSS3 features and it's REALLY tiny (7kb compressed). To use it, simply:
Within the
<head>
of your document. After that, it has a variety of functions to check for what you need. Example:There's many more ways to check for the specific HTML5/CSS3 feature you'd like. Check out their website to get it and see the docs.
是的,检查特定浏览器根本不是您想要的。它有时可用于检测何时需要应用浏览器错误的解决方法(通常使用 IE),但如果您想知道浏览器是否支持某个功能,只需嗅探即可。
有些东西比其他东西更容易嗅到。对于日期输入支持的示例,这非常简单。
input.type
属性告诉您浏览器认为它是什么类型的控件;如果不支持日期输入,您将得到'text'
。Yeah, checking for a particular browser isn't what you want at all. It's occasionally useful for detecting when you need to apply workarounds for browser bugs (usually with IE), but if you want to know whether a browser supports a feature just sniff for it.
Some things are easier to sniff than others. For your example of date-input-support it's very easy. The
input.type
property tells you what type of control the browser thinks it is; if date inputs aren't supported you'll get'text'
.