如何获取html页面中除所有其他标记之外的所有p和h标记的值 [英] how to get the value of all p and h tags in a html page excluding all other tags

查看:516
本文介绍了如何获取html页面中除所有其他标记之外的所有p和h标记的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将HTML页面加载到我的应用中。我只想显示HTML页面的内容。
请帮我一个javascript函数,我可以遍历所有 p 标签并获取< p> 的内容标记以 TextView 显示为字符串。

I am trying to load a HTML page into my app. I want to show only the content of the HTML page. please help me with a javascript function where I can loop through all the p tags and get the content of the <p> tags to display in a TextView as string.

<html><body>" +
        "<h1>First</h1><p>text text text</p>" +
        "<h1>Second</h1>more text" +
         <p>text text text</p>
           <p>text text text</p>
          <p>text text text</p>
        "</body></html>


推荐答案

如果您不想使用jQuery,请执行以下操作:

If you do not want to use jQuery just do:

var paragraphs = document.getElementsByTagName("p");
for(var i = 0; i < paragraphs.length; i++)
{
    alert(paragraphs[i].innerHTML);
}

这篇关于如何获取html页面中除所有其他标记之外的所有p和h标记的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆