简体   繁体   English

无法在iPad上聚焦新创建的元素

[英]Can not focus newly created element on ipad

I have a part of code, where I create new elements on the fly and make them focusable. 我有一部分代码,可以在其中动态创建新元素并使它们具有焦点。

The code is more complex, but here is a reproducible part on jsfiddle (when you click enter the next element is created and then focus is assigned to it): 代码更加复杂,但是这是jsfiddle的可重现部分 (当您单击Enter时,将创建下一个元素,然后将焦点分配给它):

$("body").on( "keypress", ".field", function(e) {
  if ( e.keyCode  == 13 ) {
      $(this).after('<p class="field" contenteditable="true"><br></p>')
      $(this).next().focus();
      e.stopPropagation();
      e.preventDefault();
  }
});

This works as expected in desktop browsers, but it failed to focus the next element on ipad (and may be on iphone) 这在桌面浏览器中可以正常工作,但是无法将下一个元素集中在ipad上(并且可能在iphone上)

Apparently IOS/Safari only "accepts" the focus when inside a touch event handler. 显然 ,在触摸事件处理程序中,IOS / Safari仅“接受”焦点。 I triggered a touch event and inserted the .focus() inside it. 我触发了一个触摸事件,并在其中插入了.focus() I tried this on my iPhone3S/Safari and it works: 我在我的iPhone3S / Safari上尝试了它,并且可以正常工作:

Demo . 演示

Actual Stackoverflow question . 实际Stackoverflow问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM