简体   繁体   English

从编辑器外部向WYSIWYG添加html(jQuery,ClEditor)

[英]Add html to WYSIWYG from outside the editor (jQuery, ClEditor)

I'm trying to add some html markup to the WYSIWYG CLEditor from outside of the editor itself using jQuery. 我正在尝试使用jQuery从编辑器本身外部向WYSIWYG CLEditor添加一些html标记。

So far I have... 到目前为止我有......

$('.add-image').click(
    function()
    {
        theurl = $(this).text();
        theimage = '<a href="' + theurl + '" class="lightbox"><img src="' + theurl + '" /></a>';
        // Now What? 
    }
);

But I'm at a loss as to how to add the string in to the WYSIWYG and it's starting to drive me crazy! 但我不知道如何将字符串添加到WYSIWYG中,它开始让我发疯!

This will overwrite: 这将覆盖:

$("#inputID").val(theimage); 
$("#inputID").cleditor()[0].updateFrame();

This will append: 这将附加:

currentval = $("#inputID").val();
$("#inputID").val(theimage);
$("#inputID").val(currentval + theimage); 

Or maybe try this: 或者尝试这个:

$('#inputID').val('new text data').blur();

Where inputID is the ID of your CLEditor input. 其中inputID是您的CLEditor输入的ID。

Also, this has some discussion around this: 此外,这有一些围绕这个讨论:

CLEditor dynamic adding text CLEditor动态添加文本

Just made 2 small edits to CCCasons solution to make it work as intended. 只需对CCCasons解决方案进行2次小编辑,使其按预期工作。

$('.add-image').click(
function()
{
    theurl = $(this).text();
    theimage = '<a href="' + theurl + '" class="thelightbox" style="display: block"><img src="' + theurl + '" /></a><br/>';

    // Get the current value of the textarea otherwise it will be overwritten
    currentval = $("textarea.wysiwyg").val();

    $("textarea.wysiwyg").val(currentval + theimage); 
    $("textarea.wysiwyg").cleditor()[0].updateFrame();                  
}
);

1) Added a line break to the end of the inserted link. 1)在插入的链接末尾添加换行符。 Otherwise when you try to type in the wysiwyg after adding the image it inputs inside the link. 否则,当您尝试在添加图像后输入wysiwyg时,它会在链接内输入。

2) Grabbed the current value of the textarea first to stop it being overwritten by the image. 2)首先抓取textarea的当前值以阻止它被图像覆盖。

Again, thanks a lot to CCCason! 再次,非常感谢CCCason!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM