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

 


免責聲明!

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



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