简体   繁体   English

如何获得内容可编辑div的适当内容

[英]how to get proper content of a contenteditable div

 $('button').on('click', function(){ let ht = $('#divstory').html(); console.log(ht); }); 
 .divstory{ background:#eee; min-height:54px; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="divstory" id="divstory" contenteditable="true"></div> <br> <button>CLICK</button> 

Write inside divstory the following: divstory编写以下内容:

323 323
525 525
727 727
979 979

And click on button . 然后点击button

The result is: 结果是:

323<div>525</div><div>727</div><div>979</div><div><br></div>

Why 323 is without div tags? 为什么323没有div标签?

And how to get new lines like this: 以及如何获得这样的新行:

<div>323</div>    
<div>525</div>  
<div>727</div>  
<div>979</div>  

 $('button').on('click', function(){ var html = $('#divstory').html(); var ht = remove_HtmlTags(html); console.log(ht); }); function remove_HtmlTags (html) { var tmp = document.createElement("DIV"); tmp.innerHTML = html; return tmp.textContent || tmp.innerText || ""; } 
 .divstory{ background:#eee; min-height:54px; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="divstory" id="divstory" contenteditable="true"></div> <br> <button>CLICK</button> 

It's becaouse contenteditable adds: 因为contenteditable添加:

<div><br/></div>

into its content 进入其内容

Change code into: 将代码更改为:

$('button').on('click', function(){    
    var ht = $('#divstory').html();
    ht = ht.replace("<br>", '');
    console.log(ht);
});

when you give input to the content editable div it is stored in single textNode, after hitting enter the new text is assigned to divElement but first textNode is as it is, means it does not change. 当您将输入内容提供给可编辑的div时,它存储在单个textNode中,按Enter键后,会将新文本分配给divElement,但第一个textNode不变,这意味着它不会更改。 You can change it manually using following example. 您可以使用以下示例手动更改它。

 $('button').on('click', function(){ let ht = $('#divstory')[0].childNodes; for(let child of ht){ if(child.constructor.name === 'Text'){ let newChild = document.createElement('div'); newChild.textContent = child.textContent; $('#divstory')[0].replaceChild(newChild, child); } } console.log($('#divstory').html()); }); 
 .divstory{ background:#eee; min-height:54px; white-space: pre; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="divstory" id="divstory" contenteditable="true"></div> <br> <button>CLICK</button> 

If you don't care about preserving line breaks, you can replace .html() with .text() to return just the text with no html. 如果您不关心保留换行符,则可以将.html()替换为.text()以仅返回不带html的文本。

If you want to preserve line breaks, but also not return any html, ie you'll need to replace the div and br html elements with the textual line breaks using the character /n . 如果要保留换行符,但也不返回任何html,即您需要使用字符/ndivbr html元素替换为文本换行符。 Here is a solution that works for both console or html output: 这是适用于控制台或html输出的解决方案:

 $('button').on('click', function(){ let ht = $('#divstory').html().replace(/<div>/g,"\\n").replace(/<\\/div>/g,"").replace(/<br>/g,"\\n"); console.log(ht); $("#result").text(ht); }); 
 .divstory{ background:#eee; min-height:54px; } #result{ white-space:pre; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="divstory" id="divstory" contenteditable="true"></div> <br> <button>CLICK</button> <br> <div id="result"></div> 

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

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