使用 jQuery 从 CKEditor 的 iframe 中获取内容

发布于 2024-07-21 06:57:33 字数 854 浏览 6 评论 0原文

我有一个自定义编写的 CMS,它使用 CKEditor *(FCKEditor v3) 编辑内容。 我还使用 jQuery 验证用于在基于 AJAX 的提交之前检查所有字段是否有错误的插件。 我使用 serialize() 函数来传递数据到 PHP 后端。

问题是,序列化设法正确获取所有字段,除了在 CKEditor 中输入的实际内容。 与其他所见即所得编辑器一样,该编辑器也将 iframe 覆盖在现有文本框上。 Serialize 会忽略 iframe,只在文本框中查找内容,当然,它找不到内容,因此返回空白内容正文。

我的方法是在 CKEditor 的 onchange 事件上创建一个挂钩,并同时更新文本框 (CKEDITOR.instances.[textboxname].getData()< /strong> 返回内容)或一些其他隐藏字段以及编辑器中所做的任何更改。

然而,由于 CKEditor 仍处于测试阶段并且严重缺乏文档,我找不到合适的 API 调用来帮助我做到这一点。

有谁知道如何解决这个问题?

I have a custom-written CMS that uses CKEditor *(FCKEditor v3) for editing content. I'm also using the jQuery Validation plugin to check all fields for error prior to AJAX-based submission. I'm using the serialize() function to passing the data to the PHP backend.

Problem is, serialize manages to grab all fields correctly, except for the actual content typed in CKEditor. Like every other WYSIWYG editor, this one too overlays an iframe over an existing textbox. And serialize ignores the iframe and looks only into the textbox for content, which, of course, it doesn't find, thus returning a blank content body.

My approach to this is to create a hook onto the onchange event of CKEditor and concurrently update the textbox (CKEDITOR.instances.[textboxname].getData() returns the content) or some other hidden field with any changes made in the editor.

However, since CKEditor is still in it's beta stage and severely lacks documentation, I can't find a suitable API call that'll enable me to do so.

Does anyone have any idea on how to go about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

祁梦 2024-07-28 06:57:33

另一个通用解决方案是每当您尝试提交表单时运行以下命令

for ( instance in CKEDITOR.instances )
            CKEDITOR.instances[instance].updateElement();

这将强制表单中的所有 CKEDITOR 实例更新其各自的字段

Another generic solutions to this would be to run the following whenever you try to submit the form

for ( instance in CKEDITOR.instances )
            CKEDITOR.instances[instance].updateElement();

This will force all CKEDITOR instances in the form to update their respective fields

南街九尾狐 2024-07-28 06:57:33

我刚刚发布了一个 jQuery 的 CKEditor 插件,它将在后台处理所有这些,无需任何额外的代码:
http://www.fyneworks.com/jquery/CKEditor/

I've just released a CKEditor plugin for jQuery which will take care of all this in the background without any extra code:
http://www.fyneworks.com/jquery/CKEditor/

江心雾 2024-07-28 06:57:33

我今天也一直在努力解决这个问题。 我意识到上面的代码对我不起作用的原因是因为引用文档属性时 CKEditor 实例尚未准备好。 因此,您必须调用“instanceReady”事件,并且在该事件中可以使用文档的事件,因为在此之前它并不存在。

这个例子可能适合你:

CKEDITOR.instances["editor1"].on("instanceReady", function()
{
//set keyup event
this.document.on("keyup", CK_jQ);

 //and paste event
this.document.on("paste", CK_jQ);
});

function CK_jQ()
{

    CKEDITOR.tools.setTimeout( function()
    { 
        $("#editor1").val(CKEDITOR.instances.editor1.getData()); 
    }, 0);
}

I have also been trying to fix this problem today. I realised that the reason the above code isnt working for me is because the CKEditor instance isnt ready yet when the document property is referenced. So you have to call the "instanceReady" event and within that the document's events can be used, because prior to that it just doesnt exist.

This example might work for you:

CKEDITOR.instances["editor1"].on("instanceReady", function()
{
//set keyup event
this.document.on("keyup", CK_jQ);

 //and paste event
this.document.on("paste", CK_jQ);
});

function CK_jQ()
{

    CKEDITOR.tools.setTimeout( function()
    { 
        $("#editor1").val(CKEDITOR.instances.editor1.getData()); 
    }, 0);
}
明月夜 2024-07-28 06:57:33

这应该可以做到...

CKEDITOR.instances["editor1"].document.on('keydown', function(event)
{
    CKEDITOR.tools.setTimeout( function()
    { 
        $("#editor1").val(CKEDITOR.instances.editor1.getData()); 
    }, 0);
});

CKEDITOR.instances["editor1"].document.on('paste', function(event)
{
    CKEDITOR.tools.setTimeout( function()
    { 
        $("#editor1").val(CKEDITOR.instances.editor1.getData()); 
    }, 0);
});

编辑:添加部分以在粘贴后更新文本框...

This should do it...

CKEDITOR.instances["editor1"].document.on('keydown', function(event)
{
    CKEDITOR.tools.setTimeout( function()
    { 
        $("#editor1").val(CKEDITOR.instances.editor1.getData()); 
    }, 0);
});

CKEDITOR.instances["editor1"].document.on('paste', function(event)
{
    CKEDITOR.tools.setTimeout( function()
    { 
        $("#editor1").val(CKEDITOR.instances.editor1.getData()); 
    }, 0);
});

edit: added section to update textbox after pastes, too...

满地尘埃落定 2024-07-28 06:57:33

我在这方面取得了成功:

console.log(CKEDITOR.instances.editor1.getData());

I had success with this:

console.log(CKEDITOR.instances.editor1.getData());
ゞ记忆︶ㄣ 2024-07-28 06:57:33

我采取了稍微不同的方法,我认为使用 ckeditor 的更新功能会更好,并且由于使用了 keyup,因此不需要超时

CKEDITOR.instances["editor1"].on("instanceReady", function()
{
//set keyup event
this.document.on("keyup", CK_jQ);

 //and paste event
this.document.on("paste", CK_jQ);
}

function CK_jQ()
{
   CKEDITOR.instances.editor1.updateElement(); 
}

I took a slightly different approach I figured it would be better to use ckeditor's update function and since keyup was being used the timeout wasn't needed

CKEDITOR.instances["editor1"].on("instanceReady", function()
{
//set keyup event
this.document.on("keyup", CK_jQ);

 //and paste event
this.document.on("paste", CK_jQ);
}

function CK_jQ()
{
   CKEDITOR.instances.editor1.updateElement(); 
}
羁绊已千年 2024-07-28 06:57:33

这样做不是更好吗:

CKEDITOR.instances.editor1.on('contentDom', function() {
          CKEDITOR.instances.editor1.document.on('keyup', function(event) {/*your instructions*/});
        });

ref: http:// /cksource.com/forums/viewtopic.php?f=11&t=18286

Wouldn't it be better to do just this:

CKEDITOR.instances.editor1.on('contentDom', function() {
          CKEDITOR.instances.editor1.document.on('keyup', function(event) {/*your instructions*/});
        });

ref: http://cksource.com/forums/viewtopic.php?f=11&t=18286

岁月苍老的讽刺 2024-07-28 06:57:33

contentDom 事件对我有用,而不是 instanceReady...我真的很想知道事件 ae 是什么,但我认为它们是专有的...

var editor = CKEDITOR.replace('editor');

CKEDITOR.instances.editor.on("instanceReady", function(){
    this.on('contentDom', function() {
        this.document.on('keydown', function(event) {
            CKEDITOR.tools.setTimeout( function(){ 
                $(".seldiv").html(CKEDITOR.instances.editor.getData()); 
            }, 1);
        });
    });
    this.on('contentDom', function() {
        this.document.on('paste', function(event) {
            CKEDITOR.tools.setTimeout( function(){ 
                $(".seldiv").html(CKEDITOR.instances.editor.getData()); 
            }, 1);
        });
    });
    edits_clix();
    var td = setTimeout("ebuttons()", 1);
})

The contentDom event worked for me and not the instanceReady... I would really like to know what the events ae, but I assume they are proprietary...

var editor = CKEDITOR.replace('editor');

CKEDITOR.instances.editor.on("instanceReady", function(){
    this.on('contentDom', function() {
        this.document.on('keydown', function(event) {
            CKEDITOR.tools.setTimeout( function(){ 
                $(".seldiv").html(CKEDITOR.instances.editor.getData()); 
            }, 1);
        });
    });
    this.on('contentDom', function() {
        this.document.on('paste', function(event) {
            CKEDITOR.tools.setTimeout( function(){ 
                $(".seldiv").html(CKEDITOR.instances.editor.getData()); 
            }, 1);
        });
    });
    edits_clix();
    var td = setTimeout("ebuttons()", 1);
})
怪异←思 2024-07-28 06:57:33

CKEDITOR.instances.wc_content1.getData() 将返回 ckeditor 数据
CKEDITOR.instances.wc_content1.setData() 将设置 ckeditor 数据

CKEDITOR.instances.wc_content1.getData() will return the ckeditor data
CKEDITOR.instances.wc_content1.setData() will set the ckeditor data

左耳近心 2024-07-28 06:57:33

我认为用户在询问序列化,我在序列化要提交的表单时遇到了困难,这给我带来了很多问题。

这对我有用:

$(document).ready(function() {
$('#form').submit(function(){
if ( CKEDITOR.instances.editor1.getData() == '' ){
    alert( 'There is no data available' );//an alert just to check if its working
}else{
    var editor_data = CKEDITOR.instances.editor1.getData();
    $("#editor1").val(editor_data); //at this point i give the value to the textarea
    $.ajax({ 
                    //do your ajax here  

                     });

        }
return false;
    });
 });

I think the user was asking about serializing, I was struggling serializing a form to submit and it was giving me a lot of issues.

This is what worked for me:

$(document).ready(function() {
$('#form').submit(function(){
if ( CKEDITOR.instances.editor1.getData() == '' ){
    alert( 'There is no data available' );//an alert just to check if its working
}else{
    var editor_data = CKEDITOR.instances.editor1.getData();
    $("#editor1").val(editor_data); //at this point i give the value to the textarea
    $.ajax({ 
                    //do your ajax here  

                     });

        }
return false;
    });
 });
破晓 2024-07-28 06:57:33

我用当前版本解决了这个问题: http://ajax. aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js

第 55 行之后 this.submit( function( event ) { - 我添加了这段代码:

if (typeof CKEDITOR !== "undefined") {
    for ( instance in CKEDITOR.instances )
        CKEDITOR.instances[instance].updateElement();
}

I solved this problem with the current version: http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js

After line 55 this.submit( function( event ) { - i added this code:

if (typeof CKEDITOR !== "undefined") {
    for ( instance in CKEDITOR.instances )
        CKEDITOR.instances[instance].updateElement();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文