-1
我在尝试识别中的文字。很热从一堆html标记中提取数据?
ckData
包含来自用户输入的Ckeditor的HTML标记和表格。
例如
text here texts here texts here
second line texts...etc.
<br>
<table>
...
</table>
more texts here...
<table>
....
</table>
我希望能够从ckData得到所有文本。
我可以得到表文本用以下代码
var contents = {};
var temp = document.createElement('div');
var instance = this;
temp.innerHTML = ckData;
var tables = temp.getElementsByTagName('table')
//use tables as array...
for(var i = 0; i<tables.length; i++){
var table = tables[i];
contents.rowsCount = $('tr', table).length;
contents.columnsCount = $('td', table).length/question.rowsCount;
contents.texts='';
$(table).find('td').each(function(){
contents.push($(this).text().trim());
contents.texts += $(this).text()
})
}
我希望能够提取表而外面的文本保持ckData的原始顺序。
所以最终contents
可变我希望得到的是这样的:
first part of texts
table cell data //got this
second part of texts
table cell data //got this
我得到了table cell
数据,但我不知道怎么去table
结构的texts
之外。
我希望我解释得很好。十分感谢你的帮助!
为什么不使用'temp.textContent'(注意你将失去格式)? –
@PaulS。谢谢,但我需要维护表格以外的文本的HTML标记。 – FlyingCat
[如何获取不属于子项的容器内的文本]的可能重复(http://stackoverflow.com/questions/12819953/how-to-get-text-inside-of-container-that-is-非儿童部分)...这是讽刺的,[可以如何选择jQuery文本节点?]的可能副本(http://stackoverflow.com/questions/298750/how-do-i-select -text-nodes-with-jquery)... duplicateception – Ohgodwhy