[英]wrap first line of contenteditable in div
I have a div with the attribute contentEditable set. 我有一个设置了contentContentable属性的div。
The trouble I am having is that the first line I type does not get wrapped in tags, but the subsequent lines do. 我遇到的麻烦是,我键入的第一行没有被包装在标签中,但随后的行被包裹了。
Input 输入项
abcd
efgh
ijkl
Result 结果
<div contentEditable="true">
"abcd"
<div>efgh</div>
<div>ijkl</div>
</div>
Try this : get text
and children
then make editable div empty and wrap text with div (I have added color:red to just know div added) and children. 试试这个:获取text
和children
然后将可编辑的div设为空,并用div(我已经添加了color:red来添加div)和孩子来包装文本。
$(function(){
$('div[contentEditable="true"]').each(function(){
var text = $(this).clone().children().remove().end().text();// get text
var $children = $(this).children();// get children
//add text and children again
$(this).empty().append('<div style="color:red;">'+text+'</div>').append($children);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.