获取 WP Tinymce 的内容

发布于 2024-11-02 19:08:50 字数 403 浏览 3 评论 0原文

我正在尝试编写一个 WordPress 插件。我将在 WP 的 Tinymce 编辑器中计算单词数。 基本上,它是一个字计数器,可以计算您帖子的长度并在元框中向您提供此消息

您的帖子有 450 个字

我唯一的问题是通过 javascript 从 Tinymce 获取单词。这不起作用:

document.getElementById('content')

Tinymce 的内容 id 是 content 。但这段代码返回NULL。我找不到 Tinymce 的有效 ID 名称。

很快,其他所有代码都准备好了,只是我无法从 Wordpress 的所见即所得编辑器中获取单词。

谢谢。

I'm trying to write a Wordpress plugin. I will get counts words which in WP's Tinymce editor.
Basically, it's a word counter which counting long of your post and giving you this message in a meta box

Your post has 450 words

My just problem is getting words from Tinymce via javascript. This isn't working :

document.getElementById('content')

Tinymce's content's id is content . But this code returning NULL. I couldn't find valid id name for Tinymce.

In shortly, other all codes are ready, just i can't get words from Wordpress' WYSIWYG editor.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

私野 2024-11-09 19:08:50

尝试:

tinymce.activeEditor.getContent();

tinymce.editors.content.getContent();

其中“内容”是您的文本区域的 ID。

同样,如果您只想获取 TinyMCE 文本区域中选定的(突出显示的)文本,您可以这样做:

tinymce.activeEditor.selection.getContent();

完整的 API 在这里:http://tinymce.moxiecode.com/wiki.php/API3:class.tinymce.Editor

TinyMCE 还提供了许多可以绑定的事件,特别是在您的情况下 < a href="http://tinymce.moxiecode.com/wiki.php/API3%3aevent.tinymce.Editor.onKeyUp">keyup, keydown

确保仅在 TinyMCE 加载到页面后才调用此内容。

Try:

tinymce.activeEditor.getContent();

or

tinymce.editors.content.getContent();

Where "content" is the id of your textarea.

Similarly, if you wanted to get just the selected (highlighted) text in the TinyMCE text area you would do:

tinymce.activeEditor.selection.getContent();

The full API is here: http://tinymce.moxiecode.com/wiki.php/API3:class.tinymce.Editor

TinyMCE also offers a lot of events you can bind to, particularly in your case the keyup, keydown, and keypressed events.

Be sure to call this stuff only after TinyMCE has loaded on the page.

活雷疯 2024-11-09 19:08:50

接受的答案确实对我有用,但对于一页上的多个编辑器,我必须通过编辑器 ID 访问它,所以下面

tinymce.editors['content_id'].getContent();

对我有用。

Accepted answer did work for me but for multiple editors on one page I have to access it via editor id, so below

tinymce.editors['content_id'].getContent();

Worked for me.

眼趣 2024-11-09 19:08:50

我记得小型 MCE 从 ajax 动态加载内容,所以也许您的 document.getElementById('content') 尝试过早获取该元素。

我认为你有两种方法来解决这个问题:

1)等待ajax事件完成,使用事件监听器,然后获取元素及其文本。

2)使用tinyMce函数获取文本区域的内容。在这里您可能会找到一些有用的提示:
http://tinymce.moxiecode.com/wiki.php/How-to_load/save_with_Ajax_in_TinyMCE

I remember that tiny MCE loads content dynamically from ajax, so maybe your document.getElementById('content') try to get that element too early.

I think you have 2 ways to solve this problem:

1) wait for the ajax event completition, with an event listener, and after that get the element and its text.

2) use tinyMce function to get the content of a text area. Here you may find some useful tips:
http://tinymce.moxiecode.com/wiki.php/How-to_load/save_with_Ajax_in_TinyMCE

江南月 2024-11-09 19:08:50

这是一个例子。无论 keyup 发生在 Visual 还是 HTML 模式下,编辑器下方的文本都会更新。

WP tinyMCE onKeyUp

php 文件中的 Visual 模式 事件:

function my_tiny_mce_before_init( $init ) {
    $init['setup'] = "function( ed ) { ed.onKeyUp.add( function( ed, e ) { repeater( e ); }); }";
    return $init;
}
add_filter( 'tiny_mce_before_init', 'my_tiny_mce_before_init' );

HTML 模式javascript 文件中的 事件:

jQuery( document ).ready( function( $ ) {

    $('<div id=look-at-it></div>').insertAfter('#postbox-container-1');

    $('#content').on('keyup', function( e ) {   
        repeater( e );
    });
});


var repeater = function ( e ) {
    var targetId = e.target.id;
    var text = '';

    switch ( targetId ) {

         case 'content':
             text = jQuery('#content').val(); 
             break;

         case 'tinymce':
             if ( tinymce.activeEditor )
                 text = tinymce.activeEditor.getContent();
             break;
    }

    jQuery('#look-at-it').html( text );
}


测试环境:

  • WordPress 3.4.2

Here is an example. The text below the editor will be updated whether the keyup occured in Visual or HTML mode.

WP tinyMCE onKeyUp

Visual mode event in the php file:

function my_tiny_mce_before_init( $init ) {
    $init['setup'] = "function( ed ) { ed.onKeyUp.add( function( ed, e ) { repeater( e ); }); }";
    return $init;
}
add_filter( 'tiny_mce_before_init', 'my_tiny_mce_before_init' );

HTML mode event in the javascript file:

jQuery( document ).ready( function( $ ) {

    $('<div id=look-at-it></div>').insertAfter('#postbox-container-1');

    $('#content').on('keyup', function( e ) {   
        repeater( e );
    });
});


var repeater = function ( e ) {
    var targetId = e.target.id;
    var text = '';

    switch ( targetId ) {

         case 'content':
             text = jQuery('#content').val(); 
             break;

         case 'tinymce':
             if ( tinymce.activeEditor )
                 text = tinymce.activeEditor.getContent();
             break;
    }

    jQuery('#look-at-it').html( text );
}


Tested on:

  • WordPress 3.4.2
浮光之海 2024-11-09 19:08:50

这对我有用:

if (jQuery("#wp-text-wrap").hasClass("tmce-active")){
    text1 = tinyMCE.activeEditor.getContent( { format : 'html' } );
}else{
    text1 = jQuery('[name="text"]').val();

其中text是tinymce编辑器的ID

This worked for me:

if (jQuery("#wp-text-wrap").hasClass("tmce-active")){
    text1 = tinyMCE.activeEditor.getContent( { format : 'html' } );
}else{
    text1 = jQuery('[name="text"]').val();

Where text is the ID of the tinymce editor

几味少女 2024-11-09 19:08:50

这似乎在我测试的每种情况下都对我有用。无论我们是从文本模式还是视觉模式开始,也无论之后更改模式并添加更多内容。

if ( tinyMCE.editors['id-of-your-editor'] ) {
    tinyMCE.editors['id-of-your-editor'].save();
    tinyMCE.editors['id-of-your-editor'].load();
    var your_content = tinyMCE.editors['id-of-your-editor'].getContent();
} else if ( document.getElementById( 'id-of-your-editor' ) ) {
    var your_content = document.getElementById( 'id-of-your-editor' ).value;
} else {
    alert ( 'Error' );
}

This is the one that seems to work for me in every situation I tested. Regardless if we start off from text mode or in visual mode and regardless of changing the modes and adding more content after that.

if ( tinyMCE.editors['id-of-your-editor'] ) {
    tinyMCE.editors['id-of-your-editor'].save();
    tinyMCE.editors['id-of-your-editor'].load();
    var your_content = tinyMCE.editors['id-of-your-editor'].getContent();
} else if ( document.getElementById( 'id-of-your-editor' ) ) {
    var your_content = document.getElementById( 'id-of-your-editor' ).value;
} else {
    alert ( 'Error' );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文