从CKeditor获取非html文本 [英] Getting non-html text from CKeditor
问题描述
在我的应用程序中,在插入新闻部分,我使用新闻内容的子字符串的新闻摘要。从用户获取新闻内容文本,我使用CKEditor和新闻摘要我使用子字符串方法获得一定长度的新闻内容。但当我使用CKEditor,我得到文本与HTML标签,而不是纯文本,当我使用子串方法,我的新闻摘要变得混乱!如何从这个控件获得原始文本?
我读了这,但我不能使用getText()方法
这样做
// getSnapshot()检索原始HTML,没有制表符,换行符等
var html = CKEDITOR.instances.YOUR_TEXTAREA_ID.getSnapshot();
var dom = document.createElement(DIV);
dom.innerHTML = html;
var plain_text =(dom.textContent || dom.innerText);
alert(plain_text);
viola,获取您想要的plain_text部分。
UPDATE / EXAMPLE
添加此javascript
< script type =text / javascript>
function createTextSnippet(){
//与前面一样,替换YOUR_TEXTAREA_ID
var html = CKEDITOR.instances.YOUR_TEXTAREA_ID.getSnapshot();
var dom = document.createElement(DIV);
dom.innerHTML = html;
var plain_text =(dom.textContent || dom.innerText);
//创建并设置一个128字符片段到隐藏的表单字段
var snippet = plain_text.substr(0,127);
document.getElementById(hidden_snippet)。value = snippet;
//返回true,确定提交表单
return true;
}
< / script>
,请将createTextSnippet作为onsubmit-handler添加到表单中,例如
< form action =xxxmethod =xxxonsubmit =createTextSnippet />
在
< form>
和< / form>
插入< input type =hiddenname =hidden_snippetid =hidden_snippetvalue =/>
提交表单时,您可以通过服务器访问hidden_snippet以及表单中的其余字段。
In my application, in insert news section, i use a sub string of news content for news Summary. for getting news content text from users,i use CKEditor and for news summary i use substring method to get a certain length of news content.but when i'm working with CKEditor i get text with html tags and not plain text and when i use substring method, my news summary become messed! how do i get raw text from this control? i read this but i can't use getText() method
解决方案do it like this
//getSnapshot() retrieves the "raw" HTML, without tabs, linebreaks etc var html=CKEDITOR.instances.YOUR_TEXTAREA_ID.getSnapshot(); var dom=document.createElement("DIV"); dom.innerHTML=html; var plain_text=(dom.textContent || dom.innerText); alert(plain_text);
viola, grab the portion of plain_text you want.
UPDATE / EXAMPLE
add this javascript
<script type="text/javascript"> function createTextSnippet() { //example as before, replace YOUR_TEXTAREA_ID var html=CKEDITOR.instances.YOUR_TEXTAREA_ID.getSnapshot(); var dom=document.createElement("DIV"); dom.innerHTML=html; var plain_text=(dom.textContent || dom.innerText); //create and set a 128 char snippet to the hidden form field var snippet=plain_text.substr(0,127); document.getElementById("hidden_snippet").value=snippet; //return true, ok to submit the form return true; } </script>
in your HTML, add createTextSnippet as onsubmit-handler to the form, eg
<form action="xxx" method="xxx" onsubmit="createTextSnippet();" />
inside the form, between
<form>
and</form>
insert<input type="hidden" name="hidden_snippet" id="hidden_snippet" value="" />
When the form is submitted, you can serverside access hidden_snippet along with the rest of the fields in the form.
这篇关于从CKeditor获取非html文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!