jQuery 你如何设置 TinyMCE textarea 元素的焦点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2212552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you set the focus of a TinyMCE textarea element?
提问by Justin
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 用于页面上的文本区域,但它在其他元素的 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 编辑器?
回答by jbnunn
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”操作中。
回答by David
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. Hope this helps.
由于我如何使用 JS 对象等,我不得不在 window.setTimeout 事件中设置它。希望这可以帮助。
回答by zzz
Focusing also works like this:
对焦也是这样工作的:
tinyMCE.get('id_of_textarea').focus()
Check out the tabfocus plugin, that's doing exactly what you want:
查看 tabfocus 插件,这正是您想要的:
回答by robyates
If you're using tinymce with JQuery. The following will work
如果您在 JQuery 中使用 tinymce。以下将起作用
$("#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.
您只能在编辑器初始化后执行此操作,因此您可能需要等待其初始化事件之一。
回答by VGruenhagen
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);
}
回答by Pranab Gupta
tinyMCE.get('Description').getBody().focus();
The above works in Firefox, Chrome and IE as well. I have not tested in other browsers.
以上也适用于 Firefox、Chrome 和 IE。我没有在其他浏览器中测试过。
回答by Erwin Odendaal
What actually works is setting an option in the configfile
(or jquery file) This option enables you to auto focus an editor instance. The value of this option should be an editor instance id. The editor instance id is the id
for the original textarea or <div>
element that got replaced.
实际有效的是在configfile
(或 jquery 文件)中设置一个选项此选项使您能够自动聚焦编辑器实例。此选项的值应为编辑器实例 ID。编辑器实例 ID 是被替换id
的原始文本区域或<div>
元素的ID 。
Example of usage of the auto_focus
option:
auto_focus
选项的使用示例:
tinyMCE.init({
//...
auto_focus : "elm1"
});
回答by Mike Trpcic
This should pass focus to the TinyMCE textarea:
这应该将焦点传递给 TinyMCE 文本区域:
$("#id_of_tinyMCE_area").focus();
回答by Maniruzzaman Akash
For Auto focus in tinymce the documentation first example says that - https://www.tinymce.com/docs/configure/integration-and-setup/#auto_focus
对于 tinymce 中的自动对焦,文档第一个示例说 - 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 Version4.7.6
这在TinyMCE 版本中对我有用4.7.6
回答by Serhii Maksymchuk
This works for me (TinyMCE version 4.7.6):
这对我有用(TinyMCE 版本 4.7.6):
tinymce.activeEditor.fire("focus")