使用javascript获取ckeditor文本区域的文本区域值
就 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
好的,这相当简单。假设您的编辑器名为“editor1”,这将为您提供有关其内容的警报:
更困难的部分是检测用户何时键入。据我所知,实际上并没有支持这样做(顺便说一句,我对文档印象不太深刻)。参见这篇文章:
http://alfonsoml.blogspot.com/2011/03/onchange -event-for-ckeditor.html
相反,我建议设置一个计时器,该计时器将使用文本区域的值不断更新第二个 div:
这似乎工作得很好。为了清楚起见,以下是整个事情:
Ok, this is fairly easy. Assuming your editor is named "editor1", this will give you an alert with your its contents:
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:
This seems to work just fine. Here's the entire thing for clarity:
至少从 CKEDITOR 4.4.5 开始,您可以为编辑器内容的每次更改设置一个侦听器,而不是运行计时器:
我意识到这对于 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:
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.
执行即可。
只需使用分配给编辑器的元素的元素
id = id
Simply execute
with element
id = id
of element assigned the editor.您可以在 JQuery 上集成一个函数
并将 ckeditor 元素 id 作为参数传递
You could integrate a function on JQuery
and passing as a parameter the ckeditor element id
我发现以下代码适用于 ckeditor 5
i found following code working for ckeditor 5
出色地。您询问了如何从文本区域获取值,但在您的示例中您正在使用输入。无论如何,我们开始吧:
如果您确实想要一个文本区域,请将您的输入类型文本更改为此
希望它有所帮助。
Well. You asked about get value from textarea but in your example you are using a input. Anyways, here we go:
If you really wanted a textarea change your input type text to this
Hope it helps.
您可以添加以下代码:
每次点击,ckeditor 字段数据将存储在 $('#ELEMENT_ID').val() 中。我已经使用过该方法并且效果非常好。 ckeditor 字段数据将实时保存并准备发送。
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.