Getting raw text content of HTML element with HTML uninterpreted

前端 未结 2 1346
南方客
南方客 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.

    0 讨论(0)
  • 2021-01-01 02:05

    I prepared some days ago a bin with some different approaches: http://jsbin.com/urazer/4/edit

    My favorite:

    var text = "<a href='#' title=\"Foo\"></a>");
    var html = text.replace(/[<&>'"]/g, function(c) {
      return "&#" + c.charCodeAt() + ";";
    });
    
    0 讨论(0)
提交回复
热议问题