如何在设备定向时丢失输入焦点

How to lose input focus when device orientates?

本文关键字:输入 焦点      更新时间:2023-09-26

我注意到,当您专注于表单元素并更改方向时,iOS设备会锁定网站的视图。

我想知道是否有办法修改已经实现的这段代码,以包括失去<input>的功能,<select>专注于横向或纵向方向?

这是我已有的代码:

// Fix HTML width issues on device orientation
window.document.addEventListener('orientationchange', function() {
  var iOS = navigator.userAgent.match(/(iPad|iPhone|iPod)/g);
  var viewportmeta = document.querySelector('meta[name="viewport"]');
  if (iOS && viewportmeta) {
    if (viewportmeta.content.match(/width=device-width/)) {
      viewportmeta.content = viewportmeta.content.replace(/width=[^,]+/, 'width=1');
    }
    viewportmeta.content = viewportmeta.content.replace(/width=[^,]+/, 'width=' + window.innerWidth);
  }
  // If you want to hide the address bar on orientation change, uncomment the next line
  window.scrollTo(0, 0);
}, false);

任何帮助将不胜感激,谢谢

尤里卡!

window.document.addEventListener('orientationchange', function(){
    if (window.orientation === 90 || window.orientation === -90){
        document.activeElement.blur();
    }
    else if(window.orientation === 0) {
        var viewportmeta = document.querySelector('meta[name="viewport"]');
        viewportmeta.content = viewportmeta.content.replace(/width=[^,]+/, 'width=' + window.innerWidth);
    }
    window.scrollTo(0, 0);
}, false);