getElementsByTagName() equivalent for textNodes

前端 未结 7 824
日久生厌
日久生厌 2020-11-22 06:33

Is there any way to get the collection of all textNode objects within a document?

getElementsByTagName() works great for Elements, but

7条回答
  •  抹茶落季
    2020-11-22 07:29

    I know you specifically asked for a collection, but if you just meant that informally and didn't care if they were all joined together into one big string, you can use:

    var allTextAsString = document.documentElement.textContent || document.documentElement.innerText;
    

    ...with the first item being the DOM3 standard approach. Note however that innerText appears to exclude script or style tag contents in implementations that support it (at least IE and Chrome) while textContent includes them (in Firefox and Chrome).

提交回复
热议问题