js控制ios端的input/textarea元素失去焦點時隱藏鍵盤

來源:互聯網
上載者:User

標籤:text   ios端   textarea   select   cli   set   targe   元素   eve   

同事在測試產品時發現這樣一個:“某些頁面擊完input框,在點空白處時,iOS裝置的鍵盤不能隱藏並且焦點也不會失去

帶著這個問題我進行了測試,發現在安卓的裝置上並沒有這種問題出現。

於是寫js進行測試給document添加一個click事件,發現了問題的原因:

安卓是可以觸發click事件的,而iPhone不會觸發。

也就是說在iOS裝置下你點擊空白的document處input並不能失去焦點。

解決辦法:

既然click不能觸發iPhone的事件,那就需要找觸屏事件來觸發一次:


objBlurFun("input");

//如果不是當前觸摸點不在input上,那麼都失去焦點function objBlurFun(sDom,time){ var time = time||300; var browser={ versions:function(){ var u = navigator.userAgent, app = navigator.appVersion; return { ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios終端 }; }(), language:(navigator.browserLanguage || navigator.language).toLowerCase() } if(browser.versions.ios){ var obj = document.querySelectorAll(sDom); for(var i=0;i<obj.length;i++){ objBlur(obj[i],time); } }} // 元素失去焦點隱藏鍵盤function objBlur(sdom,time){ var time = time||300; if(sdom){ sdom.addEventListener("focus", function(){ document.addEventListener("touchend", docTouchend,false); },false); }else{ throw new Error("objBlur()沒有找到元素"); } var docTouchend = function(event){ if(event.target!= sdom){ setTimeout(function(){ sdom.blur(); document.removeEventListener(‘touchend‘, docTouchend,false); },time); } }; }

 

js控制ios端的input/textarea元素失去焦點時隱藏鍵盤

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.