简体   繁体   English

将内容的第一行包装在div中

[英]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. 试试这个:获取textchildren然后将可编辑的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);
    });
});

Demo 演示版

I dont know know what you are asking .but you need result 我不知道你在问什么,但是你需要结果

 $("div[contentEditable=true]").text("abcd");
    $("div[contentEditable]").append("<div>efgh</div><div>ijkl</div>");

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM