将HTml字符串转换为DOM,然后将DOM转换为格式化文本 [英] Convert a HTml String to DOM and then DOM to formatted Text
问题描述
我使用了 RtfToHtml Converter 为了将一些文本打印到我的表格单元格中。它成功转换,然后我希望这个转换后的文本(geResult.NotesLong)格式化:
I have used an RtfToHtml Converter in order to print some text into my table cell. it converts successfully, then i want this converted text (geResult.NotesLong) to be formatted:
<td>
<script>{ setCommentValue(@colIndex, @rowIndex, @totalColCount, '@geResult.NotesLong', '@geResult.Color');}</script>
</td>
此javascript函数将其转换为DOM元素
this javascript function converts it into a DOM element
function setCommentValue(colIndex, rowIndex, assessmentLength, resultValue, color) {
var str2DOMElement = function (html) {
var frame = document.createElement('iframe');
frame.style.display = 'none';
document.body.appendChild(frame);
frame.contentDocument.open();
frame.contentDocument.write(html);
frame.contentDocument.close();
var el = frame.contentDocument.body.firstChild;
document.body.removeChild(frame);
return el;
}
var el = str2DOMElement(resultValue);
//...code to set elementPos...
$(".test").get(elementPos).appendChild(el);
}
在我的表格中显示如下
<DIV STYLE="text-align:Left;font-family:Segoe UI;font-style:normal;font-weight:normal;font-size:12;color:#000000;"><P STYLE="margin:0 0 0 0;"><SPAN><SPAN>Lee is an attentive listener who tunes into the task at hand </SPAN></SPAN></P><P /></DIV>
但是我希望它显示为(在上面的标签中定义了格式)
However i want it to display as (with formatting defined in the above tags)
Lee is an attentive listener who tunes into the task at hand
如何格式化此文本,使其按照标签格式化,但只有文本显示在我的表格单元格中?
How do i format this text so that it is formatted as per tags but only text is displayed in my table cell?
推荐答案
最后我找到了一种从内部提取纯文本的方法(但它仍然缺少格式)
Finally i have found a way to extract plain text from inside (but it is still missing formats)
html page
<td>
<script>{ setCommentValue(@colIndex, @rowIndex, @totalColCount, '@geResult.NotesLong', '@geResult.Color');}</script>
</td>
javascript
function setCommentValue(colIndex, rowIndex, assessmentLength, resultValue, color) {
String.prototype.replaceAll = function (search, replacement) {
var target = this;
return target.split(search).join(replacement);
};
//replace all text special characters
var replaced = resultValue.replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll(""", "\"").replaceAll("'", "\'");
var parser = new DOMParser();
var doc = parser.parseFromString(replaced, "text/xml");
....
$(".test").get(elementPos).innerHTML = doc.firstChild.textContent;
}
我的文字有特殊字符,例如> for> etc,所以我手动更换了它们。
My text had special characters like > for > etc so i replaced them manually.
这篇关于将HTml字符串转换为DOM,然后将DOM转换为格式化文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!