从html中获取包含格式的文本

get text from html including format

本文关键字:格式 文本 包含 获取 html      更新时间:2024-01-17

我想从我的html中获取文本,并像在浏览器上一样保留正确的格式。

我想要这个

               CompanyName
              CompanyAddress
           Company Phone Number
       1  Item1   7.00   0%   7.00
       2  Item2   5.00   0%   5.00
                  TOTAL:         2
               SUBTOTAL:     12.00
                   CASH:     12.00

我得到这个

                    CompanyName
                   CompanyAddress
                Company Phone Number
                   1  
                   Item1   
                  7.00   
                   0%   
                     7.00

我的HTML:

<div id="printPreview" class="interprise-popover-container">
<div class="interprise-popover-header popover-header">
    <h1 class="title">Print Preview</h1>
    <a class="popover-btn left-popover-btn btn-Cancel">Cancel</a>
    <a class="popover-btn right-popover-btn btn-Done">Done</a>
</div>
<br class="clearfloat"/>
<div id="printPreviewBody" class="interprise-popover-content">
    <div id="print-area">
    <div id="print-header">
        <h4><%= Company %></h4>
        <h6><%= WarehouseAddress %></h6>
        <h6><%= WarehouseCity %>, <%= WarehouseState %> <%= WarehousePostalCode %></h6>
        <h6><%= PhoneNumber %></h6>
        <h6>www.fairysales.com</h6>
        <h5 style="margin-top: 5px;"><%= TransactionType %>#&nbsp;<%= InvoiceCode %></h5>
    </div>
    <div id="print-barcode" style="margin:0 auto; "></div>
    <span style="font-size: 12px;">Monday</span>
    <div>
        <table id="print-items" cellspacing="0" width="100%">
                        <tbody>
                          <tr>
                            <td align="right"> 1 </td>
                            <td align="left"> Item1<br></td>
                            <td align="right">12.00</td>
                            <td align="right">0%</td>
                            <td align="right"><div><abbr>12.00</abbr></div></td>
                          </tr>
                       </tbody>
        </table>
    </div>
    <div id="print-total">
    </div>
</div>
</div>

顺便说一下,我用了

$("#print-header").text(); to extract the text from the div

更新:这是jsfiddle

http://jsfiddle.net/8yC5G/1/

我想要实现的实际上与结果相同,但采用文本格式,这样我就可以将其作为字符串从javascript传递给目标c。

我认为使用$("#print-header").html();应该可以解决您的问题。

请参阅$.text()文档和$.html()文档。

由于你的html与你所说的不一致,以及你在哪里得到的不一致。我假设你不想要条形码,你不想要"星期一",你不需要"交易类型"/"语音代码",最后没有"www.fairysales.com"
我还假设您想要<%=仓库城市%>,<%=仓库状态%><%=仓库PostalCode%>彼此相邻。

但更重要的是:我猜你控制了这个html源代码及其生成,因为你在"打印头"中使用了vars
然后我假设你也有生成你的两个表的var:"打印项目"answers"打印总数",对吧?

如果你有vars。。并且实际上想要将它们传递给"目标c",那么。。您可以控制用于将这些数据发送到objectivec应用程序以执行进一步任务的数据格式。

然而,如果你仍然希望将渲染的html文本等效为(单)间距的纯文本(例如,只在发给客户的htm电子邮件中出现的文本),作为目标c的输入,那么你需要像以前的学校一样,更多地考虑你的数据:在最坏的情况下,每个字段会有多少字符?这是您需要附加/预结束的空白字符数量(以模拟表中的右对齐/左对齐)。这并不难。

现在假设您没有权限访问构建发送到浏览器的html的vars
然后,在使用上述技术(数据格式/计算/预处理/附加)之前,您必须递归地解析html块,获取包含所需数据的"innerTEXT"/"textContent"(它还可以查看表的单元格对齐方式)。

所以。。你控制var吗?

希望这能有所帮助!

ps:我建议使用单间距字体,因为。。否则,你将如何按照发布"你想要的"的方式调整你的数据
ps2:我其实也在想。。为什么选择<div><abbr>12.00</abbr></div>

更新:我摆弄了一下你的小提琴,构建了一个函数,它可以完全满足你的需求。请参阅jsfiddle.net/8yC5G/3/
该函数的返回函数仍然很脏,可能需要进行一些优化。也许我会再摆弄一些。注:在FF中构建和测试,使用"textContent"而不是"innerTEXT"。

更新2:jsfiddle.net/8yC5G/6/:新例程,清理了一点,更短更可靠。自动缩放:P