繁体   English   中英

如何将HTML表转换为纯文本并将其保存到JavaScript中的变量中

[英]How to convert an HTML table to plain text and save it into a variable in JavaScript

我正在检查站点,发现JavaScript:如何从字符串中剥离HTML标签? 但这并不能真正说明如何进行此操作:

<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&amp;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());

那应该起作用。

jsFiddlehttp : //jsfiddle.net/hescano/EQ9b3/

暂无
暂无

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

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