Getting raw text content of HTML element with HTML uninterpreted

前端 未结 2 1345
南方客
南方客 2021-01-01 01:34

I have Googled my brains out and can\'t figure out how to make this work. Here is what I\'m trying to do:

HTML:

"Hi, my n
2条回答
  •  一整个雨季
    2021-01-01 02:02

    To quote bobince

    When you ask the browser for an element node's innerHTML, it doesn't give you the original HTML source that was parsed to produce that node, because it no longer has that information. Instead, it generates new HTML from the data stored in the DOM. The browser decides on how to format that HTML serialisation; different browsers produce different HTML, and chances are it won't be the same way you formatted it originally.

    In summary: innerHTML/innerText/text/textContent/nodeValue/indexOf, none of them will give you the unparsed text.

    The only possible way to do this is with regex, or you can do an ajax post to the page itself, but that is a bad practice.

提交回复
热议问题