获取 WP Tinymce 的内容
我正在尝试编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试:
或
其中“内容”是您的文本区域的 ID。
同样,如果您只想获取 TinyMCE 文本区域中选定的(突出显示的)文本,您可以这样做:
完整的 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:
or
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:
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.
接受的答案确实对我有用,但对于一页上的多个编辑器,我必须通过编辑器 ID 访问它,所以下面
对我有用。
Accepted answer did work for me but for multiple editors on one page I have to access it via editor id, so below
Worked for me.
我记得小型 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
这是一个例子。无论 keyup 发生在 Visual 还是 HTML 模式下,编辑器下方的文本都会更新。
php 文件中的 Visual 模式 事件:
HTML 模式javascript 文件中的 事件:
测试环境:
Here is an example. The text below the editor will be updated whether the keyup occured in Visual or HTML mode.
Visual mode event in the php file:
HTML mode event in the javascript file:
Tested on:
这对我有用:
其中text是tinymce编辑器的ID
This worked for me:
Where text is the ID of the tinymce editor
这似乎在我测试的每种情况下都对我有用。无论我们是从文本模式还是视觉模式开始,也无论之后更改模式并添加更多内容。
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.