1. 程式人生 > >兼容安卓和蘋果移動端就input調起手機相冊和相機

兼容安卓和蘋果移動端就input調起手機相冊和相機

change phone ios 蘋果手機 mce prev chan 開始 att

以下這麽寫的話,蘋果手機可以調起相機和相冊功能,但是安卓手機只能調起相冊;

<input id="upLicense" onchange="preview(this,0)" type="file" name="upLicense" >

<input id="upLicense" onchange="preview(this,0)" type="file" name="upLicense" accept="image/*" capture="camera">

而這麽寫的話,可以讓安卓手機同時調起相機和相冊,但是,蘋果手機卻只能調起相機:

<input id="upLicense" onchange="preview(this,0)" type="file" name="upLicense" accept="image/*" capture="camera" multiple>

所以,綜上結合,可以在一開始的時候這麽寫:

<input id="upLicense" onchange="preview(this,0)" type="file" name="upLicense" accept="image/*" capture="camera" multiple>

然後在頁面js中這麽寫:

$(function()){

  compatibleInput();

}

// 判斷當前是否屬於ios移動端,兼容input同時調用手機相冊和相機

function compatibleInput(){
  //獲取瀏覽器的userAgent,並轉化為小寫
  var ua = navigator.userAgent.toLowerCase();
  //判斷是否是蘋果手機,是則是true
  var isIos = (ua.indexOf(‘iphone‘) != -1) || (ua.indexOf(‘ipad‘) != -1);
  if (isIos) {
    $("input:file").removeAttr("capture");
  };
}

兼容安卓和蘋果移動端就input調起手機相冊和相機