how would I detect if “multiple” attribute is supported for file input elements?

微笑、不失礼 提交于 2019-12-01 00:43:41

问题


Internet Explorer does not support the multiple attribute for <input type="file" />. However, its not only IE that lacks this support... also certain mobile browsers do not support the multiple attribute. So simply detecting that the browser is IE is not the ideal solution.

So how would I detect if the multiple attribute is supported for for <input type="file" /> with JavaScript?

UPDATE

It seems like Modernizr has support for new HTML5 input element attributes:

http://modernizr.com/docs/#input

The accepted solution seems to work, however, since I'm already using Modernizr, my solution is the following:

/**
 * Determines if the given attribute is supported for <input /> elements.
 * 
 * @param attribute - the attribute to test for (ex. "multiple")
 */
function isInputAttributeSupported(attribute) {
    return (Modernizr.input[attribute]) ? true : false;
};

回答1:


var inp = document.createElement("input");
inp.setAttribute("multiple", "true");
var supportsMultiple = inp.multiple===true;



回答2:


You can try checking for the existence of the corresponding property:

var supportsMultipleFiles = 'multiple' in document.createElement('input');

Example: http://jsfiddle.net/sbZvS/



来源:https://stackoverflow.com/questions/10457770/how-would-i-detect-if-multiple-attribute-is-supported-for-file-input-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!