使用javascript获取ckeditor textarea的textarea值 [英] Getting the textarea value of a ckeditor textarea with javascript
问题描述
对于JS,我是一个学习者,虽然我花了好几个小时阅读通过教程,这已经帮助了很多,但我仍然有问题,弄清楚我究竟是如何找到一个用户键入a ckeditor textarea。
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.
我想要做的是,当有人输入textarea时,无论它们的类型出现在不同部分的div页面。
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.
我有一个简单的文本输入,但是因为文本区域是一个ckEditor类似的代码不工作。
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.
我知道答案在这里:ckEditor API textarea值<但是我不知道我想要做什么。
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?
我工作的代码是:
$('#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>
推荐答案
有问题确切地说明我如何找到一个
用户正在输入ckeditor文本区。
I'm still having problems figuring out exactly how I find out what a user is typing into a ckeditor textarea.
相当容易。假设你的编辑器被命名为editor1,这将给你一个警告与它的内容:
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());
更难的部分是检测用户键入的时间。从我可以告诉,实际上没有支持这样做(和我不是太深刻的文档btw)。请参阅这篇文章:
http://alfonsoml.blogspot.com/2011/ 03 / onchange-event-for-ckeditor.html
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
相反,我建议设置一个定时器,将持续更新您的第二个div of textarea:
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>
这篇关于使用javascript获取ckeditor textarea的textarea值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!