Js控制iphone端的input/textarea元素失去焦点时隐藏键盘

原文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;
            }
原文地址:https://www.cnblogs.com/lichuntian/p/6094375.html