javascript - uiwebview javascript HTML 仅获取面向用户的正文

标签 javascript iphone ios uiwebview document-body

我在 UIWebView 中加载了以下 html 源代码
我要提取
文本1
文本2 文本2
文本3 文本3 文本3

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>1322170516271</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=1, minimum-scale=1.0, maximum-scale=4.0">                   

    <style type="text/css">
    body
    {
        padding: 5px;
        margin: 0px;
        font-family: Helvetica, Arial;
        font-size: 12pt;
        background-color: #efefef;
        background-image: url(ArticleBackground.jpg);
        background-position: cover;
        color: #000000;
    }
    h1
    {
        text-align: center;
        border-bottom: 1px dotted #805050;
        font-size: 28px;
        line-height: 38px;
        margin-bottom: 30px;
        text-shadow: 0 2px 1px white;
        color: #803030;
    }
    </style>

</head>

<body>

    <script type="text/javascript">
    function printMe()
    {
        print();
    }
    </script>

    <div style='align:center; padding: 20px;'>

        <div>

    <b>text1</b><br><br>

    <h2>
      text2 text2
    </h2>
    <br>
    text3 text3 text3

        </div>

    </div>

</body>
</html>

但这是我使用时得到的结果

[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"]

我不需要 body 和 h1。我只想要面向用户的实际文本。

234534546



    body
{
    padding: 5px;
    margin: 0px;
    font-family: Helvetica, Arial;
    font-size: 12pt;
    background-color: #efefef;
    background-image: url(ArticleBackground.jpg);
    background-position: cover;
    color: #000000;
}
h1
{
    text-align: center;
    border-bottom: 1px dotted #805050;
    font-size: 28px;
    line-height: 38px;
    margin-bottom: 30px;
    text-shadow: 0 2px 1px white;
    color: #803030;
}







    function printMe()
    {
        print();
    }






text1


  text2 text2


text3 text3 text3

感谢您的任何见解。

更新

[webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"] 对我的目标也不起作用

<script type="text/javascript">
    function printMe()
    {
        print();
    }
    </script>

    <div style="align:center; padding: 20px;">

        <div>

    <b>text1</b><br><br>

    <h2>
       text2 text2
    </h2>
    <br>
    text3 text3 text3

        </div>

    </div>

更新:这是现有项目所需要的。如果我有机会重新设计它,就会很容易找到解决方案。但是考虑到这个 HTML 源代码,它可能会有点困难。

最佳答案

尝试使用:

document.body.innerHTML

或者看一下解析 HTML:parsing HTML on the iPhone SO 上还有许多其他链接。

关于javascript - uiwebview javascript HTML 仅获取面向用户的正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11009268/

相关文章:

javascript - AngularJS 中的链接选项

ios - iOS7 中的 MFMailComposeViewController 自定义

ios - SECP256K1 使用 SawTooth Swift

ios - 如何使用 Swift 将图像更改为 MKAnnotation

javascript - 通过 javascript 或 jquery 获取客户端 pc 的主机名

javascript - 使用 javascript 从客户端访问 DynamoDB?

iphone - 将富文本内容显示为 UIWebView 或 UILabel+CoreText

ios - 在 IOS 中从 FTP 获取 pdf 文件列表的步骤

ios - 无法在聊天 TableView 中创建带有聊天气泡的自定义消息 lbl。 (全部以编程方式)

javascript - 使边框布局的ExtJS中心项目水平滚动