简体   繁体   English

如何包装 <p> 在第一线的contenteditable div

[英]How to wrap with <p> in first line of contenteditable div

I used below code to wrap <p> instead of <div> . 我使用下面的代码来包装<p>而不是<div>

document.execCommand('defaultParagraphSeparator', false, 'p')

But I still can't wrap first line, like this "aaa". 但我仍然无法包装第一线,就像这个“aaa”。

<div id=“body-text” class=“body-text” contenteditable=“true” data-placeholder=“Body Contents">
    aaa
    <p>bbb</p>
    <p>ccc</p>
</div>

Does anyone know how to wrap first line "aaa" with <p> ? 有谁知道如何用<p>包装第一行“aaa”?

postscript 后记

I changed my cord using one of the answer for reference. 我用其中一个答案换了我的绳子作为参考。 But now I cant type any letter. 但现在我无法输入任何字母。 Only if I press enter first, it works. 只有先按Enter键才有效。 But after I press enter and make <p> , I cant type any letter again. 但是当我按下回车键并输入<p> ,我再也无法输入任何字母了。

Where is the problem? 问题出在哪儿?

<div id='body-text' class='body-text' contenteditable=true data-placeholder='Body Contents' onkeydown={firstLine}></div>
<script>
firstLine(e) {
    if(e.keyCode == '13') {
        var div = document.getElementById('body-text')
        var text = div.firstChild.textContents
        div.removeChild(div.firstChild)
        var p = document.createElement('p')
        p.textContent = text
        div.insertBefore(p, div.firstChild)
    }
}
</script>

One way to do it is to get first text node of your div, save the text, remove that node and then create ap tag with the text from your ancient node and insert it in your div. 一种方法是获取div的第一个文本节点,保存文本,删除该节点,然后使用古代节点中的文本创建ap标记并将其插入div中。

UPDATED 更新

you had a little typpo problem var text = div.firstChild.textContents , there is no s it only var text = div.firstChild.textContent 你有一个小的typpo问题var text = div.firstChild.textContents ,没有它只有var text = div.firstChild.textContent

see fiddle: https://jsfiddle.net/2rgLzkyj/ 看小提琴: https//jsfiddle.net/2rgLzkyj/

HTML : HTML:

<div id='body-text' class='body-text' contenteditable=true data-placeholder='Body Contents'>
  aaa
  <p>bbb</p>
</div>

Javascript: 使用Javascript:

var div = document.getElementById('body-text')

div.addEventListener('keydown', onKeyDown);

function onKeyDown(e) {
    if (e.keyCode == '13') {
        var text = div.firstChild.textContent;
        div.removeChild(div.firstChild);
        var p = document.createElement('p');
        p.textContent = text;
        div.insertBefore(p, div.firstChild);
    }
}

I'm using element.firstChild to select the "aaa" textNode, and creating the paragraph element which then gets prepended back into .body-text . 我正在使用element.firstChild来选择“aaa”textNode,并创建段落元素,然后将其添加到.body-text

 var pElement = document.createElement('p'); var bodyText = document.querySelector('.body-text'); var firstLine = bodyText.firstChild; pElement.appendChild(firstLine); bodyText.prepend(pElement); console.log(bodyText.outerHTML) 
 <div class="body-text" contenteditable="true" data-placeholder="Body Contents"> aaa <p>bbb</p> <p>ccc</p> </div> 

note: you don't need a class and an ID... choose one 注意: 您不需要课程和ID ...选择一个

Ok, first I have to let you know that the code you provided was using those fancy quotes and you must use either this " or this ' . Having said that, I have made a Snippet that uses formatBlock . 好的,首先我要告诉你,你提供的代码是使用那些花哨的引号你必须使用这个"或者这个' 。话虽如此,我制作了一个使用formatBlock的Snippet。

  1. Highlight the text and click the button <p/> . 突出显示文本,然后单击按钮<p />
  2. It can wrap text in a <p> , <div> , <blockquote> , <h1> , etc.. 它可以在<p><div><blockquote><h1>等中包装文本。

SNIPPET SNIPPET

 <div id="body-text" class="body-text" contenteditable="true" data-placeholder="Body Contents"> aaa <p>bbb</p> <p>ccc</p> </div> <input type="button" class="p" onclick="document.execCommand('formatBlock', false, 'p')" value="<p/>" title="Insert a Paragraph Wrapped on Highlighted Text"> 

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

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