我正在检查该网站并发现JavaScript: How to strip HTML tags from string?但这并没有真正解释如何采取这一点:
<tr id="element.incident.comments.additional">
<td colspan="2">
<span style="">
<table cellpadding="0" cellspacing="0" style="table-layout:fixed" width="100%">
<tbody>
<tr>
<td colspan="2">
<hr>
</td>
</tr>
<tr style="">
<td class="tdwrap"><strong>2014-01-23 14:45:40 - <a style="color:blue" href="sys_user.do?sysparm_view=itil&sysparm_query=user_name=Superhero@superhero.com">SuperHero</a></strong></td>
<td align="right" nowrap="true"><sup>Additional comments</sup></td></tr>
<tr style="">
<td colspan="2"><span style="word-wrap:break-word;">received from: SDUperhero@superhero.com<br><br>lalalalalala
<br>lotsofwords<br><br><br><br><br><br>
The information transmitted, including any attachments, is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material</span></td></tr></tbody></table></span></td>
</tr>
并获取里面的文本:
<tr id="element.incident.comments.additional">
用于进一步解析。
我尝试过
function strip(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent||tmp.innerText;
}
var commentsField = document.getElementById("element.incident.comments.additional").innerHTML;
alert(strip(commentsField));
但我不确定这是否是正确的方法,因为我没有在警报中收到任何内容。
有什么想法吗?
最佳答案
因为你的 id 中有 .
,所以你需要在 jQuery 选择器中转义它们:
$("#txt").val($("#element\\.incident\\.comments\\.additional").html());
这应该可行。
jsFiddle:http://jsfiddle.net/hescano/EQ9b3/
关于javascript - 如何将 HTML 表格转换为纯文本并将其保存到 JavaScript 中的变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21320764/