Getting raw text content of HTML element with HTML uninterpreted

前端 未结 2 1359
南方客
南方客 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:05

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

    My favorite:

    var text = "");
    var html = text.replace(/[<&>'"]/g, function(c) {
      return "&#" + c.charCodeAt() + ";";
    });
    

提交回复
热议问题