Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

javascript - wrap the line from <div contenteditable=true> with <p> tag in keyup/keypress event

While typing content in <div contenteditable=true id='edited_content>, find whether the line have <p> tag if not wrap it with <p> tag. If header tag presents, should not wrap it with <p> tag.

In google chrome browser, after adding header tag content, instead of wrap content with <p> tag, wrapping it with <div>. How to prevent this?

My code:

var timeoutReference;
$("#edited_content").keyup(function(e) {
  var code;
  code = (e.keyCode ? e.keyCode : e.which);
  if (code === 13) {
    $("#edited_content").contents().filter(function() {
      return this.nodeType === 3 && $.trim(this.nodeValue).length;
    }).wrap("<p />");
  } else {
    if (timeoutReference) {
      clearTimeout(timeoutReference);
    }
    timeoutReference = setTimeout(function() {
      $("#edited_content.publitory").contents().filter(function() {
        return this.nodeType === 3 && $.trim(this.nodeValue).length;
      }).wrap("<p />");
    }, 3000);
  }
});

While start typing, wrap current line with <p> tag and give enter then next line should wrap with <p> tag. In my code, I did setTimeout for 3 seconds, if stop typing for 3 seconds then wrap that line with <p> tag. But my client ask to remove setTimeout, he wants to wrap the current line with <p> immediately. How to achieve this?

typing html content:

test content
<h2>sub content</h2>
content1

Resultant html content after wraping it with <p> tag:

<p>Test content</p>
<h2>sub content</h2>
<p>content1</p>

But in chrome browser:

<p>test content</p>
<h2>sub content</h2>
<div>content1</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...