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


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

<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");
  };
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM