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失去焦点,隐藏光标。