javascript 使用 jQuery 隐藏 TinyMCE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6114818/
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
Hiding TinyMCE with jQuery
提问by 23890123890
I have a TinyMCE textarea inside of #container
我里面有一个 TinyMCE textarea #container
When I use $('#container').hide()
and then $('#container').show()
, tinyMCE throws:
当我使用$('#container').hide()
and then 时$('#container').show()
,tinyMCE 抛出:
Cannot read property 'selection' of undefined
无法读取未定义的属性“选择”
I'm using the jquery plugin, so this is how I set it up:
我正在使用 jquery 插件,所以我是这样设置的:
$('#container textarea').tinymce({ /* options */ });
What should I be doing differently?
我应该怎么做?
回答by Thariama
The correct command to use here is
此处使用的正确命令是
// editor_id is the id of your textarea and
// tinymce will use this id to uniquely identify this editor instance
editor_id = $("#container textarea").attr('id');
tinymce.get(editor_id).hide();
to make it visible again use
使其再次可见,使用
tinymce.get(editor_id).show();
回答by Learner
This question is about hiding and showing tinymce editor but if anyone came here about removing and re-adding tinymce editor without error then my solution can work for them.
这个问题是关于隐藏和显示 tinymce 编辑器的,但是如果有人来这里讨论删除和重新添加 tinymce 编辑器而没有错误,那么我的解决方案可以为他们工作。
To remove existing tinymce editor and add new needs clearance of tinymce.EditorManager.editors array. This solution works in both cases : 1. If you have only one editor and you want to remove and add it again. 2. If you have multiple editors and you want to remove some special editor and add it again.
删除现有的 tinymce 编辑器并添加新的需要清除 tinymce.EditorManager.editors 数组。此解决方案适用于两种情况: 1. 如果您只有一个编辑器并且您想删除并重新添加它。2. 如果您有多个编辑器,并且您想删除一些特殊的编辑器并重新添加它。
console.log(tinymce.EditorManager.editors);
This will give you a view of the array and exact index of you desired editor which you want to remove. For example one sample output of above console can be:
这将为您提供要删除的所需编辑器的数组和确切索引的视图。例如,上述控制台的一个示例输出可以是:
Array[2]
0:B
1:B
length:2
textarea-1:B
textarea-2:B
_proto_Array[0]
This is the output of the console when i have two tinymce editors on textareas : #textarea-1 and #textarea-2 Lets suppose I want to delete #textarea-2 and re-add it then it can be done as follows:
这是当我在 textareas 上有两个 tinymce 编辑器时控制台的输出:#textarea-1 和 #textarea-2 假设我想删除 #textarea-2 并重新添加它然后可以按如下方式完成:
tinymce.EditorManager.editors.splice(1, 1);//removing second element in array.
delete tinymce.EditorManager.editors['textarea-2'];//deleting respective textarea id from array
Then you can add it again simply using init:
然后你可以简单地使用 init 再次添加它:
tinymce.init({
selector:'#ts-textarea-2'
});
If you have only one textarea associated with tinymce editor lets say : #textarea-1 and you want to remove and re-initialize it then you can just empty tinymce.EditorManager.editors by :
如果您只有一个与 tinymce 编辑器关联的 textarea 可以说:#textarea-1 并且您想删除并重新初始化它,那么您可以通过以下方式清空 tinymce.EditorManager.editors:
tinymce.EditorManager.editors = [];
And then you can add using init command as explained above. Worked for me without any error.
然后你可以添加使用 init 命令,如上所述。对我来说有效没有任何错误。
I hope it helps
我希望它有帮助
回答by Chris Bricker
Instead of hiding it, try sending it off screen - something like:
与其隐藏它,不如尝试将其发送到屏幕外——例如:
$('#container').css('left', '-1000px');
EDIT/UPDATE:
编辑/更新:
You could also try removing TinyMCE from the textarea before you hide() the container, and then bring it back after you show(). But you'll need to give your textarea an #ID:
您也可以尝试在 hide() 容器之前从 textarea 中删除 TinyMCE,然后在 show() 之后将其带回来。但是你需要给你的 textarea 一个#ID:
//To Enable
tinyMCE.execCommand('mceAddControl', false, $("#container textarea").attr('id'));
//To Disable
tinyMCE.execCommand('mceRemoveControl', false, $("#container textarea").attr('id'));
回答by 23890123890
Apparently it was the animation. if I show()/hide() I'm fine, but when I try to animate in tinyMCE has an issue after I finish animating, possibly trying to set options once the textarea's display isn't none.
显然是动画。如果我显示()/隐藏()我很好,但是当我在完成动画后尝试在 tinyMCE 中设置动画时会出现问题,可能会在 textarea 的显示不是 none 后尝试设置选项。