Is there a way to get all text from the rendered page with JS?

后端 未结 3 900
感动是毒
感动是毒 2021-01-02 14:32

Is there an (unobtrusive, to the user) way to get all the text in a page with Javascript? I could get the HTML, parse it, remove all tags, etc, but I\'m wondering if there\'

3条回答
  •  迷失自我
    2021-01-02 15:19

    I suppose you could do something like this, if you don't mind loading jQuery.

    var theText;
    $('p,h1,h2,h3,h4,h5').each(function(){
      theText += $(this).text();
    });
    

    When its all done, "theText" should contain most of the text on the page. Add any relevant selectors I may have left out.

提交回复
热议问题