如何检测浏览器是否支持HTML5功能

4
我希望通过javascript/jQuery检测用户的浏览器是否支持input type='file'标签的multiple属性,该属性是HTML5添加多个图像的功能。
<input type=file  name="images" value=" " multiple=""/>

请告诉我如何做到这一点。
5个回答

6
var i = document.createElement("input");
if (typeof i.multiple !== 'undefined') {
    // supports multiple
}

1
如果您只需要测试此属性,则无需使用Modernizr。 - alexn

2

您可以使用专门为此目的开发的 modernizr

http://modernizr.com/

您可以通过以下方式检测是否支持某个特性,例如 placeholder

if (!Modernizr.input.placeholder) {

}

你能给我展示一些代码吗?在我将modernizr.js包含到我的页面之后。 - Rusi Nova

1

从 Modernizr 源代码中获取并编辑:

var inputElem = document.createElement('input');
var multipleSupport = !!(multiple in inputElem);

如果这是你唯一需要的东西,你不需要包含整个库。


0
你可以使用Modernizr,它可以为你完成大量的测试。
但是,在这种情况下,测试非常简单:
if (typeof document.createElement('input').multiple !== "undefined") {
    // ... yes, it has it
}

同样,对于元素上的所有其他新属性也是如此。


0

我建议您使用Modernizr JavaScript库。这提供了一种检查浏览器是否支持各种功能的方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接