簡體   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