我使用 textEditor PrimeFaces 元素遇到了同样的问题。
我使用以下 html 代码。
<div contenteditable="true" class="div-focus">
<h:outputText
styleClass="OutputField"
value="#{oController.panelTpl.getComment()}"
escape="false"
/>
</div>
<div class="text-editor">
<p:textEditor
value="#{oController.panelTpl.txtComment.text}"
/>
</div>
TextArea 定义了两次,因此当显示表单时,用户只能看到第一个 <div> 小部件,而不会看到特定的工具栏。
当用户点击显示的文本时,focusin() 事件隐藏 <div class="div-focus"> 元素并显示 <div class="text-editor"> 父元素中包含的 <p:textEditor> 小部件。
为此,我定义了以下 javascript 代码
function onLoadDialog()
{
jQuery(".text-editor").hide();
jQuery(".div-focus").focusin(function()
{
$(this).hide();
$(this).next(".text-editor").show();
$(this).next(".text-editor").focus();
});
jQuery(".text-editor").focusout(function(e)
{
if ($(e.relatedTarget).closest(".text-editor").size() == 0)
{
$(this).hide();
$(this).prev(".div-focus").show();
$(this).prev(".div-focus").text($(this).text());
}
});
}
onLoadDialog 函数()在页面加载时被调用,用于隐藏 <div class="text-editor> 元素以及定义的 focusin() 和 focusout() 事件。
focusin() 事件隐藏 <div class="div-focus"> 元素并显示 <div class="text-editor"> 元素。当用户点击 <div class="div-focus"> 元素中的文本时,该元素被隐藏并显示以下隐藏元素。<div class="text-editor"> 元素获得焦点。
focusout() 事件隐藏 <div class="text-editor"> 元素并显示 <div class="div-focus"> 元素...仅当获得焦点的元素未在 <div class="text-编辑器">元素。
element.focusout() 的问题在于每次包含在主元素中的元素时都会触发它,即使获得焦点的元素是在同一个元素中定义的。
当您进入房屋时,您可以通过车库或客厅或厨房进入。当您进入客厅(通过外门)时,您就进入了大楼。但是当你离开客厅去厨房时,你就呆在家里了!!!如果你离开客厅去花园,你就离开了房子(建筑)。
focusin() 函数实现相同的原理,但不是 focusout() !
if ($(e.relatedTarget).closest(".text-editor").size() == 0) 在 focusout() 事件中使用的行纠正了这个小差异!
注意:该解决方案并不完美,因为在将文本从 textEditor 小部件复制到 outputText 小部件而不进行格式化时,我有一些小问题需要解决!