在 tinyMCE 编辑器中,我自动输入的文本用 包裹<p>
,这很好。我只想为它添加一个类<p>
。
注意:我不想为所有 p 元素添加一个类,只是用户单击并开始输入的那个。
任何帮助都非常感谢。
为此,您应该使用 tinymce javascript API:
editor = tinymce.get('your_editor_id');
$(editor.selection.getNode()).parents('p:first').addClass('your_class');
这是一个非jquery解决方案:
editor = tinymce.get('your_editor_id');
editor.selection.getNode().closest("p:first").setAttribute("class", "your_class");
试试这个:
var tmceIframe = $(".mceIframeContainer iframe")[0].contentDocument || $(".mceIframeContainer iframe")[0].contentWindow.document;
$(tmceIframe).find("p").addClass("test")
或一行:
$($("iframe")[0].contentDocument || $("iframe")[0].contentWindow.document).find("p").addClass("test")
在第二个示例中,我通过直接进入 iframe 使其更短,但是,如果您在页面上有多个 iframe,这可能是一个问题,您可能希望使用元素 id 来指定,例如在包装器上