专注于加载时 iframe 中的内容可编辑正文

focusing on a contenteditable body in an iframe on load

本文关键字:正文 编辑 iframe 于加载 加载 专注      更新时间:2023-09-26

这在Firefox和IE中运行良好,但我无法在Chrome中使其工作:

self.editorIframeDocument.body.focus()

editorIframeDocument == contentDocument || contentWindow.document取决于浏览器。

.body.focus()使编辑器加载时我可以立即开始输入。完美,但是,在Chrome中它不起作用。我尝试挂接到iframe以及iframe #document但没有成功。

我看到了一些设置选择并删除它的技巧,但它们都是<div> contenteditable中的示例,里面有特定的 HTML,而不是一个可以包含从无到大量文本的<iframe>

没有jQuery。

问题是编辑器还没有加载,所以Chrome不会让我专注于它。检查就绪状态解决了以下问题:

if (self.settings.focusOnLoad) {
  // We need to wait until all three iframes are done loading by waiting until the parent
  // iframe's ready state == complete, then we can focus on the contenteditable
  self.iframe.addEventListener('readystatechange', function () {
    if (self.iframe.readyState == 'complete') {
      self.editorIframeDocument.body.focus();
    }
  });
}