使用 jQuery 操作 TinyMCE 内容
使用 TinyMCE,我可以轻松地操作内容并将其发送回编辑器,如下所示:
// get content from tinyMCE
var content = tinyMCE.get('content').getContent();
// manipulate content using js replace
content = content.replace(/<\/?div>/gi, '');
// send back to tinyMCE
tinyMCE.get('content').setContent( content );
上面的代码运行良好。但是,我无法让它发挥作用:
// get content from tinyMCE (it provides an html string)
var content = tinyMCE.get('content').getContent();
// make it into a jQuery object
var $content = $(content);
// manipulate the jquery object using jquery
$content = $content.remove('a');
// use a chained function to get its outerHTML
content = $("<div />").append( $content.clone() ).html();
// send back to tinyMCE
tinyMCE.get('content').setContent( content );
我的方法有问题吗?
With TinyMCE, I can easily manipulate content and send it back to the editor, like this:
// get content from tinyMCE
var content = tinyMCE.get('content').getContent();
// manipulate content using js replace
content = content.replace(/<\/?div>/gi, '');
// send back to tinyMCE
tinyMCE.get('content').setContent( content );
The above code works fine. However, I can't get this to work:
// get content from tinyMCE (it provides an html string)
var content = tinyMCE.get('content').getContent();
// make it into a jQuery object
var $content = $(content);
// manipulate the jquery object using jquery
$content = $content.remove('a');
// use a chained function to get its outerHTML
content = $("<div />").append( $content.clone() ).html();
// send back to tinyMCE
tinyMCE.get('content').setContent( content );
Is there something wrong with my methodology?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
设置和访问 TinyMCE 是正确的;问题出在我使用
.remove()
上:由于 TinyMCE 中的内容是单个对象,而不是对象集合,其中一些对象是
< ;a>
标签,该操作没有效果,返回的 html 与原始的相同。为了删除链接,我需要这个:
我在此线程中收到了澄清: $('#foo').remove('a') 和 $('#foo').find('a').remove() 之间的区别
The setting and getting to TinyMCE was correct; the problem was with my use of
.remove()
:Since the content from TinyMCE was a single object, and not a collection of objects some of which were
<a>
tags, that manipulation had no effect, and the html that returned was the same as the original.In order to remove links, I instead needed this:
I received clarification in this thread: Difference between $('#foo').remove('a') and $('#foo').find('a').remove()
您需要使用获取编辑器内容
并使用设置内容
You need to get the editor content using
and set the content using