用新内容替换特定 dom 元素

Replace specific dom element with new content

我有这样的结构:

<div class="foo">
  <div class="bar">
    Some content
  </div>
  Maybe some other content
</div>

我定义了一个自定义插件来编辑这个部分。而我return用新的html内容来代替div.foo.

我将新内容设置为:

tinyMCE.activeEditor.selection.setContent(html_content);

我与新插件相关的自定义按钮适用于:foobar div。我的意思是分配了相同的行为。

问题在于保存:我想 select div.foo dom 元素并将其替换为新的 html_content。因此,而不是设置当前内容(不确定是什么)select如何在 TinyMCE v.3 中设置特定 dom 元素的内容?

(更新:我不在乎用户在按下 MyCustomButton 之前 select 编辑了什么,如果 foo div 中的某些内容 select 编辑了整个部分被新内容替换。)

似乎只使用一些 jQuery 代码:

var $old_section = $("iframe").contents().find('div.foo').first();
$old_section.replaceWith(html_content);

有待改进:使 .foo 最接近给定的选择。