How to strip HTML tags with jQuery?

后端 未结 5 944
别跟我提以往
别跟我提以往 2020-11-27 04:12

I want to remove HTML tags from a string. For example assume we have the string:

 

example ive got a string

How can I wr

5条回答
  •  爱一瞬间的悲伤
    2020-11-27 04:34

    The safest way is to rely on the browser TextNode to correctly escape content. Here's an example:

    function stripHTML(dirtyString) {
      var container = document.createElement('div');
      var text = document.createTextNode(dirtyString);
      container.appendChild(text);
      return container.innerHTML; // innerHTML will be a xss safe string
    }
    
    document.write( stripHTML('

    some content

    ') ); document.write( stripHTML('
    提交回复
热议问题