[英]Paste html from textarea to Contenteditable Div
I'm developing a RTE(rich text editor) using a wrapper div. 我正在使用包装器div开发RTE(富文本编辑器)。
<div id="myeditor"></div>
//then
editorfunction(myeditor);
What the function does is add the following elements
<div id="myeditor">
<div class="toolbar">buttons here</div>
<div class="editorwrapper">
<div class="editor-richtext">
rich text etc
</div>
<textarea class="editor-source"><p>rich text etc</p></textarea>
</div>
</div>
I can successfully grab the html from the .editor-richtext and put it inside the textarea, but when I edit the textarea, I don't seem to be able to paste that back into the rich-text. 我可以成功地从.editor-richtext中获取html并将其放在textarea中,但是当我编辑textarea时,我似乎无法将其粘贴回富文本中。
Thanks in advance! 提前致谢!
Update 1 更新1
Ok, it seems that 好吧,好像
$("richtext").blur(function() {
$("textarea").val($(this).html());
});
Works fine, but not the other way around (from textarea to richtext). 工作正常,但不是相反(从textarea到richtext)。
Update 2 It seems it is very unstable, it partially works but is acting strange :\\ I'm not able to fully get content from textarea and paste as html into contenteditable. 更新2它似乎非常不稳定,它部分工作但行为奇怪:\\我无法从textarea完全获取内容并粘贴为html成为contenteditable。 I will continue to do some research.
我会继续做一些研究。
Update 3 I just updated update 1 and update 2 as I totally flipped textarea and richtext in my brain. 更新3我刚刚更新了更新1并更新了2,因为我在脑中完全翻转了textarea和richtext。 Sorry!
抱歉!
Update 4 Ok, I pretty much got it solved now. 更新4好的,我现在几乎已经解决了。 I just have one slight problem, upon initialization, if I don't focus the contenteditable div and switch to the source view\\textarea.
我只是有一个小问题,在初始化时,如果我不关注contenteditable div并切换到源视图\\ textarea。 the textarea is emptied, and when I then go back to RTE view\\contenteditable div it is emptied.
textarea被清空,当我回到RTE视图\\ contenteditable div时,它被清空。 from the empty textarea\\source.
来自空的textarea \\ source。 I'm working on a work-around.
我正在努力解决问题。
You can hook the onBlur event of textarea to copy the text and paste it in editor-richtext 您可以挂钩textarea的onBlur事件来复制文本并将其粘贴到editor-richtext中
$("textarea.editor-source").blur(function(){
$("div.editor-richtext").html($(this).val());
});
EDIT 编辑
For other way around, you can use the following code segment 换句话说,您可以使用以下代码段
$("textarea.editor-source").focus(function(){
$(this).val($("div.editor-richtext").text());
});
You may want to use jQuery and the following functionnalities? 您可能想使用jQuery和以下功能?
$(".editor-source").keyup(function() {
$(".editor-richtext").html($(this).val());
});
Everything works fine. 一切正常。 Selectors in your example are incorrect thought:
您的示例中的选择器是错误的想法:
HTML: HTML:
<div class="editor-richtext">
original text
</div>
<textarea class="editor-source">modified text</textarea>
JS: JS:
$(".editor-source").blur(function() {
$(".editor-richtext").html($(this).val());
});
UPD: UPD:
$(".editor-richtext").click(function(){
$(".editor-source").val($(this).html().trim());
});
new demo that puts content from div
into textarea
on click event. 新的演示是把内容从
div
到textarea
上单击事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.