原文http://www.it165.net/pro/html/201404/12672.html
IPHONE系統在點擊document時textarea和input沒有失去焦點
function objBlur(obj, time){ if(typeof obj != 'string') return false; var obj = document.getElementById(obj), time = time || 300, docTouchend = function(event){ if(event.target!= obj){ setTimeout(function(){ obj.blur(); document.removeEventListener('touchend', docTouchend,false); },time); } }; if(obj){ obj.addEventListener('focus', function(){ //注釋這部分是在一個頁面多個這樣的調用時禁止冒泡讓他不要讓ios默認輸入框上下彈,最好寫在對應頁面里給對應元素寫這里效率低,這種寫法很差所以先注釋掉下次優化再貼 // var input = document.getElementsByTagName('input'), // ilength = input.length; // for(var i=0; i<ilength; i++){ // input[i].addEventListener('touchend',function(e){e.stopPropagation()},false); // } // var textarea = document.getElementsByTagName('textarea'), // tlength = textarea.length; // for(var i=0; i<tlength; i++){ // textarea[i].addEventListener('touchend',function(e){e.stopPropagation()},false); // } document.addEventListener('touchend', docTouchend,false); },false); }else{ //找不到obj } }
調用方法如下 var isIPHONE = navigator.userAgent.toUpperCase().indexOf('IPHONE')!= -1; if(isIPHONE){ var input = o.objBlur, input2 = new input('realName'), input3 = new input('telphone'), input4 = new input('address'), input2 = input3 = input4 = null; }
