var isIPHONE = navigator.userAgent.toUpperCase().indexOf('IPHONE')!= -1; // 元素失去焦點隱藏iphone的軟鍵盤 function objBlur(obj,time){ var that; // if(typeof id != 'string') throw new Error('objBlur()參數錯誤'); time = time || 100, docTouchend = function(event){ if(event.target!= obj){ setTimeout(function(){ if (typeof($(event.target).attr("readonly"))!=="undefined") { obj[that].blur(); document.removeEventListener('touchend', docTouchend,false); return false; } },time); } }; if(obj){ for (var i = 0; i<obj.length; i++) { obj[i].index = i; obj[i].addEventListener('focus', function(){ that = this.index; document.addEventListener('touchend', docTouchend,false); },false); } }else{ throw new Error('objBlur()沒有找到元素'); } }
調用:
$(function () { if(isIPHONE){ var obj = document.getElementsByTagName('input'); var input = new objBlur(obj); input=null; } })
說明:焦點在某個input元素上,此時快速滑動頁面,由於是absolute定位,光標跟不上滾動的速度。導致錯位問題。
解決方案就是在你滑動頁面的時候直接讓input失去焦點,隱藏光標。這里對測試人員吐槽一下:尼瑪,好好的表單你不填,你非要滑動頁面玩,XX。。。
不過話說回來,要不是測試員奇葩,我也不會學到更多東西,嘻嘻!還是非常感謝的!Bugs,come on!! 測試→