getting contents with tinyMCE?

感情迁移 提交于 2019-12-23 18:52:30

问题


I have a tinyMCE textarea #frmbody and am using the jquery instance of it.

<textarea class="tinymce" name="frmbody" id="frmbody" cols="30" rows="20"></textarea>

I'm trying to get the contents of the textarea as the user types.

$("#frmbody").live('keyup', function(e) {
    alert("keyup");
});

The code above is not working and I'm not sure why. If I remove the tinyMCE instance, the code above works fine. Any ideas?


回答1:


That's because an instance of TinyMCE isn't a true textarea, so keyup events aren't detected. Try the onchange callback instead.




回答2:


You can make tinyMCE own listener by: http://www.tinymce.com/wiki.php/API3:event.tinymce.Editor.onKeyUp

or write your own and use built-in function getContent: http://www.tinymce.com/wiki.php/API3:method.tinymce.Editor.getContent




回答3:


One could just grab the contents of the editor using :

tinymce.activeEditor.getContent();

If you want to attach an onchange event, according to this page, http://www.tinymce.com/wiki.php/api4:class.tinymce.Editor, create the editor using the following code :

var ed = new tinymce.Editor('textarea_id', { 
    init_setting_item: 1,
    ...
}, tinymce.EditorManager);

ed.on('change', function(e) {
    var content = ed.getContent();
    $("#textarea_id").val(content); // update the original textarea with the content
    console.log(content);
});

ed.render();

Please note that onchange is fire when the user unfocuses, press enter, or press a toolbar button instead of every keystroke.




回答4:


It is very easy to write an own handler and assign it to the editor iframe document. There are tinymce handlers for various events already defined like keyUp

Here is the standard way to assign an own handler to the editor iframe document

var my_handler = function(event) {
    alert('my handler fired!');
};

var ed = tinymce.get('my_editor_id');
$(ed.getDoc()).bind('keyup', my_handler);



回答5:


TinyMCE hides the Text Area and creates a html container. If you write content in the box its a iFrame with the name "TEXTAREANAME_ifr".

So try this:

$("#frmbody_ifr").live('keyup', function(e) {
    alert("keyup");
});

I think as Thariama already said the EventHandler from TinyMCE would be the best way.



来源:https://stackoverflow.com/questions/9895421/getting-contents-with-tinymce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!