简体   繁体   English

你如何设置TinyMCE textarea元素的焦点?

[英]How do you set the focus of a TinyMCE textarea element?

I'm using TinyMCE for a textarea on a page but it doesn't play nice in the tabbing order of the other elements. 我在页面上使用TinyMCE作为textarea,但它在其他元素的Tab键顺序中不能很好用。

I can use the following code to capture when I tab out of the first element: 当我跳出第一个元素时,我可以使用以下代码捕获:

$('#title').live('keypress', function (e) {
   if(e.keyCode == 9) {
       alert('tabbed out');
   }
});

How can I set the focus to a TinyMCE editor? 如何将焦点设置为TinyMCE编辑器?

Finally found an answer... use the command: 终于找到了答案...使用命令:

tinymce.execCommand('mceFocus',false,'id_of_textarea');

For my purposes, 'id_of_texterea' was "description", ie 就我的目的而言,'id_of_texterea'是“描述”,即

<textarea id="description" ... ></textarea>

In the form element that preceded my textarea, I added the execCommand above to the element's "onblur" action. 在我的textarea之前的表单元素中,我将上面的execCommand添加到元素的“onblur”操作中。

I know this is an old post, but just to add my input about having the editor open and focus not working. 我知道这是一篇旧帖子,但只是添加了关于让编辑器打开并且无法正常工作的输入。 What I found that worked for me was this: 我发现对我有用的是:

tinyMCE.activeEditor.focus();

I had to set this in a window.setTimeout event because of how I was using JS objects and such. 我必须在window.setTimeout事件中设置它,因为我使用JS对象等。 Hope this helps. 希望这可以帮助。

Focusing also works like this: 聚焦也是这样的:

tinyMCE.get('id_of_textarea').focus()

Check out the tabfocus plugin, that's doing exactly what you want: 看看tabfocus插件,它正是你想要的:

http://tinymce.moxiecode.com/tryit/tab_focus.php http://tinymce.moxiecode.com/tryit/tab_focus.php

If you're using tinymce with JQuery. 如果你正在使用jQuery的tinymce。 The following will work 以下将有效

$("#id_of_textarea").tinymce().focus();

You can only do this after the editor has initialized though and so you may need to wait on one of its initialization events. 您只能在编辑器初始化之后执行此操作,因此您可能需要等待其中一个初始化事件。

What actually works is setting an option in the configfile (or jquery file) This option enables you to auto focus an editor instance. 实际工作原理是在configfile (或jquery文件)中设置一个选项。此选项使您可以自动聚焦编辑器实例。 The value of this option should be an editor instance id. 此选项的值应为编辑器实例ID。 The editor instance id is the id for the original textarea or <div> element that got replaced. 编辑器实例id是原始textarea或被替换的<div>元素的id

Example of usage of the auto_focus option: auto_focus选项的使用示例:

tinyMCE.init({
    //...
    auto_focus : "elm1"
});

Looks like for TinyMCE 4 a few of these solutions no longer work. 对于TinyMCE 4来说,这些解决方案中的一些不再有效。

// Doesn't seem to work on TinyMCE 4 //似乎不适用于TinyMCE 4

tinymce.execCommand('mceFocus',false,'id_of_textarea'); 

// Didn't work //没用

$("id_of_textarea").tinymce().focus();

tinyMCE.activeEditor.focus();

What works for me 什么对我有用

// Works great in Chrome, but not at all in Firefox //在Chrome中运行良好,但在Firefox中根本不适用

tinyMCE.init({
    //...
    auto_focus : "id_of_textarea"
});

// Add this so Firefox also works //添加这个,这样Firefox也可以

init_instance_callback : "customInitInstanceForTinyMce", // property to add into tinyMCE.init()

function customInitInstanceForTinyMce() {
    setTimeout(function () {                         // you may not need the timeout
        tinyMCE.get('Description').focus();
    }, 500);
}
tinyMCE.get('Description').getBody().focus();

The above works in Firefox, Chrome and IE as well. 以上工作也适用于Firefox,Chrome和IE。 I have not tested in other browsers. 我没有在其他浏览器中测试过。

这应该将焦点转移到TinyMCE textarea:

$("#id_of_tinyMCE_area").focus();

For Auto focus in tinymce the documentation first example says that - https://www.tinymce.com/docs/configure/integration-and-setup/#auto_focus 对于自动对焦,文档的第一个例子说 - https://www.tinymce.com/docs/configure/integration-and-setup/#auto_focus

tinymce.init({
      selector: '#textarea_id',
      auto_focus: '#textarea_id'
});

This works for me in TinyMCE Version 4.7.6 这在TinyMCE版本 4.7.6适用于我

我已经通过使用以下方法与TinyMCE 4.x合作:

tinymce.EditorManager.get('id_of_editor_instance').focus();

I use the following function to focus the editor with thinyMCE This is using jQuery, but would be easy to strip this out and use document.querySelectorAll() . 我使用以下函数将编辑器与thinyMCE集中在一起这是使用jQuery,但很容易将其删除并使用document.querySelectorAll() Use babel if you need this in es5. 如果你需要在es5中使用babel

let focusEditor = function(editorContainer) {
  let tinymce;
  if (/iPad|iPhone|iPod/.test(navigator.platform)) { return; }
  let id = $('.text-editor', editorContainer.innerHTML).first().attr('id');
  if (tinymce = window.tinyMCE.get(id)) {
    try {
      return tinymce.focus();
    } catch (error) {
      return tinymce.on('init', function() { return this.focus(); });
    }
  } else {
    return $(`#${id}`, editorContainer).focus();
  }
}

The editorContainer is any element surrounding the tinyMCE textarea, eg a div with id 'text-container' you could call focusEditor($('#text-container')) or in React focusEditor(React.findDOMNode(this)) editorContainer是tinyMCE textarea周围的任何元素,例如id为'text-container'的div,你可以调用focusEditor($('#text-container'))或者在React focusEditor(React.findDOMNode(this))

这适用于我(TinyMCE版本4.7.6):

tinymce.activeEditor.fire("focus")

TinyMCE 4.7.4 TinyMCE 4.7.4

None of the above worked for me, subscribing to TinyMCEs init event did work: 以上都没有为我工作,订阅TinyMCEs init事件确实有效:

const elTinyMce = tinyMCE.get('your_textarea_id');
// (optional) This is not necessary.
// I do this because I may have multiple tinymce elements on my page
tinyMCE.setActive(elTinyMce);
elTinyMce.on('init', function () {
    // Set the focus
    elTinyMce.focus();
    // (optional) Select the content
    elTinyMce.selection.select(
        elTinyMce.getBody(),
        true
    );
    // (optional) Collapse the selection to start or end of range
    elTinyMce.selection.collapse(false);
});

Note: focusing on elements doesn't always work if you have the browsers developer tools open. 注意:如果您打开浏览器开发人员工具,则关注元素并不总是有效。 You may be forcing the focus away from the page because a browser can only have one focus. 您可能会将焦点从页面上移开,因为浏览器只能有一个焦点。 This solution for example doesn't work either if you have developer tools open. 例如,如果您打开了开发人员工具,此解决方案就不起作用。

如何集中注意力<textarea>&lt;i&gt;element that is located in shadowRoot in javascript or jquery?&lt;/div&gt;&lt;/i&gt;&lt;b&gt;位于 javascript 或 jquery 中 shadowRoot 的元素?&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> DOM 结构如下所示:</p><p> <a href="https://i.stack.imgur.com/VXxr1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VXxr1.png" alt="在此处输入图像描述"></a></p><p> 我只想将focus()事件赋予这个 Textarea 元素,但它不起作用。</p><p> 我尝试的代码快照是:</p><pre> var email_text_box_pr = $('kat-textarea'); var email_text_box_sr = email_text_box_pr[0].shadowRoot; email_text_box = $(email_text_box_sr).find('textarea'); email_text_box.focus();</pre><p> 希望尽快得到任何人的帮助。</p><p> 问候</p></div> - How to focus the <textarea> element that is located in shadowRoot in javascript or jquery?

暂无
暂无

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

相关问题 如何在元素上设置默认的focus() - How do you set a default focus() on an element 无法在iPad上将焦点设置为tinyMCE textarea - Can't set focus to tinyMCE textarea on iPad tinyMCE JQuery插件-如何设置全局配置选项? - tinyMCE JQuery plugin - How do you set global config options? JQuery:如何将FOCUS设置为TinyMCE编辑器? - JQuery: How to set FOCUS to a TinyMCE editor? 使用jQuery,如何在调整大小后使元素保持焦点? - With jquery how do you keep an element in focus after resize? 在焦点上,对文本区域进行自定义焦点 - On focus, do customized focus on textarea 提交后将重点放在tinyMCE上 - set focus on tinyMCE after submit 设置TinyMce编辑器的重点 - Set focus onload of TinyMce editor 如何使用jQuery设置select html元素的selected元素? - How do you set the selected element of a select html element with jQuery? 如何集中注意力<textarea>&lt;i&gt;element that is located in shadowRoot in javascript or jquery?&lt;/div&gt;&lt;/i&gt;&lt;b&gt;位于 javascript 或 jquery 中 shadowRoot 的元素?&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> DOM 结构如下所示:</p><p> <a href="https://i.stack.imgur.com/VXxr1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VXxr1.png" alt="在此处输入图像描述"></a></p><p> 我只想将focus()事件赋予这个 Textarea 元素,但它不起作用。</p><p> 我尝试的代码快照是:</p><pre> var email_text_box_pr = $('kat-textarea'); var email_text_box_sr = email_text_box_pr[0].shadowRoot; email_text_box = $(email_text_box_sr).find('textarea'); email_text_box.focus();</pre><p> 希望尽快得到任何人的帮助。</p><p> 问候</p></div> - How to focus the <textarea> element that is located in shadowRoot in javascript or jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM