使用javascript获取ckeditor文本区域的文本区域值

发布于 2024-12-12 02:36:15 字数 849 浏览 0 评论 0原文

就 JS 而言,我是一个学习者,尽管我花了几个小时阅读教程,这对我有很大帮助,但我仍然无法弄清楚如何准确地找出用户在 ckeditor 文本区域中输入的内容。

我想做的是拥有它,这样当有人在文本区域中输入内容时,他们输入的任何内容都会显示在页面不同部分的 div 中。

我有一个简单的文本输入可以很好地执行此操作,但由于文本区域是 ckEditor,因此类似的代码不起作用。

我知道答案就在这里: ckEditor API textarea value 但我不知道我知道的不够多,无法弄清楚我要做什么。我想没有人愿意帮助我吧?

我工作的代码是

$('#CampaignTitle').bind("propertychange input", function() {
  $('#titleBar').text(this.value);
});

<label for="CampaignTitle">Title</label>
<input name="data[Campaign][title]" type="text" id="CampaignTitle" />

<div id="titleBar" style="max-width:960px; max-height:76px;"></div>

I'm a learner as far as JS goes and although I've spent a good few hours reading through tutorials which has helped lots but I'm still having problems figuring out exactly how I find out what a user is typing into a ckeditor textarea.

What I'm trying to do is have it so that when someone types into the textarea, whatever they type appears in a div in a different part of the page.

I've got a simple text input doing that just fine but because the text area is a ckEditor the similar code doesn't work.

I know the answer is here: ckEditor API textarea value but I don't know enough to figure out what I'm meant to do. I don't suppose anyone fancies helping me out?

The code I've got working is:

$('#CampaignTitle').bind("propertychange input", function() {
  $('#titleBar').text(this.value);
});

and

<label for="CampaignTitle">Title</label>
<input name="data[Campaign][title]" type="text" id="CampaignTitle" />

and

<div id="titleBar" style="max-width:960px; max-height:76px;"></div>

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

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

发布评论

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

评论(8

暖阳 2024-12-19 02:36:15

我仍然无法弄清楚如何找到一个
用户正在 ckeditor 文本区域中输入内容。

好的,这相当简单。假设您的编辑器名为“editor1”,这将为您提供有关其内容的警报:

alert(CKEDITOR.instances.editor1.getData());

更困难的部分是检测用户何时键入。据我所知,实际上并没有支持这样做(顺便说一句,我对文档印象不太深刻)。参见这篇文章:
http://alfonsoml.blogspot.com/2011/03/onchange -event-for-ckeditor.html

相反,我建议设置一个计时器,该计时器将使用文本区域的值不断更新第二个 div:

timer = setInterval(updateDiv,100);
function updateDiv(){
    var editorText = CKEDITOR.instances.editor1.getData();
    $('#trackingDiv').html(editorText);
}

这似乎工作得很好。为了清楚起见,以下是整个事情:

<textarea id="editor1" name="editor1">This is sample text</textarea>

<div id="trackingDiv" ></div>

<script type="text/javascript">
    CKEDITOR.replace( 'editor1' );

    timer = setInterval(updateDiv,100);
    function updateDiv(){
        var editorText = CKEDITOR.instances.editor1.getData();
        $('#trackingDiv').html(editorText);
    }
</script>

I'm still having problems figuring out exactly how I find out what a
user is typing into a ckeditor textarea.

Ok, this is fairly easy. Assuming your editor is named "editor1", this will give you an alert with your its contents:

alert(CKEDITOR.instances.editor1.getData());

The harder part is detecting when the user types. From what I can tell, there isn't actually support to do that (and I'm not too impressed with the documentation btw). See this article:
http://alfonsoml.blogspot.com/2011/03/onchange-event-for-ckeditor.html

Instead, I would suggest setting a timer that is going to continuously update your second div with the value of the textarea:

timer = setInterval(updateDiv,100);
function updateDiv(){
    var editorText = CKEDITOR.instances.editor1.getData();
    $('#trackingDiv').html(editorText);
}

This seems to work just fine. Here's the entire thing for clarity:

<textarea id="editor1" name="editor1">This is sample text</textarea>

<div id="trackingDiv" ></div>

<script type="text/javascript">
    CKEDITOR.replace( 'editor1' );

    timer = setInterval(updateDiv,100);
    function updateDiv(){
        var editorText = CKEDITOR.instances.editor1.getData();
        $('#trackingDiv').html(editorText);
    }
</script>
他夏了夏天 2024-12-19 02:36:15

至少从 CKEDITOR 4.4.5 开始,您可以为编辑器内容的每次更改设置一个侦听器,而不是运行计时器:

CKEDITOR.on("instanceCreated", function(event) {
    event.editor.on("change", function () {
        $("#trackingDiv").html(event.editor.getData());
    });
});

我意识到这对于 OP 来说可能为时已晚,并且不会显示为正确答案或有任何投票(尚未),但我想我会为未来的读者更新这篇文章。

At least as of CKEDITOR 4.4.5, you can set up a listener for every change to the editor's contents, rather than running a timer:

CKEDITOR.on("instanceCreated", function(event) {
    event.editor.on("change", function () {
        $("#trackingDiv").html(event.editor.getData());
    });
});

I realize this may be too late for the OP, and doesn't show as the correct answer or have any votes (yet), but I thought I'd update the post for future readers.

仲春光 2024-12-19 02:36:15

执行即可。

CKEDITOR.instances[elementId].getData();

只需使用分配给编辑器的元素的元素 id = id

Simply execute

CKEDITOR.instances[elementId].getData();

with element id = id of element assigned the editor.

蹲在坟头点根烟 2024-12-19 02:36:15

您可以在 JQuery 上集成一个函数

jQuery.fn.CKEditorValFor = function( element_id ){
  return CKEDITOR.instances[element_id].getData();
}

并将 ckeditor 元素 id 作为参数传递

var campaign_title_value = $().CKEditorValFor('CampaignTitle');

You could integrate a function on JQuery

jQuery.fn.CKEditorValFor = function( element_id ){
  return CKEDITOR.instances[element_id].getData();
}

and passing as a parameter the ckeditor element id

var campaign_title_value = $().CKEditorValFor('CampaignTitle');
病毒体 2024-12-19 02:36:15

我发现以下代码适用于 ckeditor 5

ClassicEditor
    .create( document.querySelector( '#editor' ) )
    .then( editor => {
        editor.model.document.on( 'change:data', () => {
            editorData = editor.getData();
        } );
    } )
    .catch( error => {
        console.error( error );
    } );

i found following code working for ckeditor 5

ClassicEditor
    .create( document.querySelector( '#editor' ) )
    .then( editor => {
        editor.model.document.on( 'change:data', () => {
            editorData = editor.getData();
        } );
    } )
    .catch( error => {
        console.error( error );
    } );
小嗷兮 2024-12-19 02:36:15

出色地。您询问了如何从文本区域获取值,但在您的示例中您正在使用输入。无论如何,我们开始吧:

$("#CampaignTitle").bind("keyup", function()
{
    $("#titleBar").text($(this).val());
});

如果您确实想要一个文本区域,请将您的输入类型文本更改为此

<textarea id="CampaignTitle"></textarea>

希望它有所帮助。

Well. You asked about get value from textarea but in your example you are using a input. Anyways, here we go:

$("#CampaignTitle").bind("keyup", function()
{
    $("#titleBar").text($(this).val());
});

If you really wanted a textarea change your input type text to this

<textarea id="CampaignTitle"></textarea>

Hope it helps.

烈酒灼喉 2024-12-19 02:36:15

您可以添加以下代码:
每次点击,ckeditor 字段数据将存储在 $('#ELEMENT_ID').val() 中。我已经使用过该方法并且效果非常好。 ckeditor 字段数据将实时保存并准备发送。

$().click(function(){
    $('#ELEMENT_ID').val(CKEDITOR.instances['ELEMENT_ID'].getData());
});

you can add the following code :
the ckeditor field data will be stored in $('#ELEMENT_ID').val() via each click. I've used the method and it works very well. ckeditor field data will be saved realtime and will be ready for sending.

$().click(function(){
    $('#ELEMENT_ID').val(CKEDITOR.instances['ELEMENT_ID'].getData());
});
謸气贵蔟 2024-12-19 02:36:15
var campaignTitle= CKEDITOR.instances['CampaignTitle'].getData();
var campaignTitle= CKEDITOR.instances['CampaignTitle'].getData();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文