简体   繁体   English

Froala编辑器:将光标集中在页面加载上

[英]Froala editor: focus cursor on page load

I thought something like this would be simple, but apparently not. 我觉得这样的事情很简单,但显然不是。 I'm using a paid version of the Froala Editor, and I love it. 我正在使用Froala编辑器的付费版本,我喜欢它。 However, it needs to focus the cursor when the page loads inside the editor so I don't have to force my users to manually click it. 但是,当页面在编辑器中加载时,它需要聚焦光标,因此我不必强迫用户手动单击它。

This is the code from their official docs, but it doesn't seem to work. 这是他们官方文档中的代码,但它似乎不起作用。

# HTML
<textarea id="task_body" autofocus="true">...</textarea>

# JavaScript
editor = $("#task_body").froalaEditor({});
editor.froalaEditor("events.focus");

All I want to do is focus the cursor in the Froala editor field when the page loads; 我想要做的就是在页面加载时将光标聚焦在Froala编辑器字段中; I'd even be happy with a JS solution that calls a focus event after the page loads. 我甚至对在页面加载后调用焦点事件的JS解决方案感到满意。

To set focus at Froala init(), do as follows: 要在Froala init()上设置焦点,请执行以下操作:

$('div#froala-editor')
 .on('froalaEditor.initialized', function (e, editor) {
  editor.events.focus(true);
}).froalaEditor({
  ......editor init settings...
})

This uses the jquery event mechanism to capture the froalaEditor.initialized event and then users the passed-in editor object to call for focus. 这使用jquery事件机制来捕获froalaEditor.initialized事件,然后使用传入的编辑器对象来调用焦点。

To assign focus programatically, meaning any time after Froala init(), use 要以编程方式分配焦点,意味着在Froala init()之后的任何时间,请使用

$('selector').froalaEditor('events.focus', true).

For example, here I use a delay to simulate time passing between Froala init() and the focus call: 例如,在这里我使用延迟来模拟Froala init()和焦点调用之间的时间传递:

  setTimeout(function(){
    $('#edit').froalaEditor('events.focus', true);
      console.clear()
      console.log('fired focus trigger!')
  }, 2000)

Whilst this is useful to know, it is not the gist of the current Froala API documentation which does not provide this insight and seems instead to hint at the syntax being 虽然知道这一点很有用,但这并不是当前Froala API文档的要点,它没有提供这种见解,而是暗示提示语法是

$('#selector').froalaEditor('events.trigger', 'focus'); $('#selector')。froalaEditor('events.trigger','focus');

which I could not get to work. 我无法上班。 Maybe Froala should consider clarifying the documentation. 也许Froala应该考虑澄清文档。

Thanks to Stefan at Froala support who promptly responded to my call for assistance. 感谢Froala支持的Stefan,他迅速回复了我的求助电话。

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

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