How to decode HTML entities using jQuery?

后端 未结 19 2531
忘了有多久
忘了有多久 2020-11-21 23:21

How do I use jQuery to decode HTML entities in a string?

19条回答
  •  半阙折子戏
    2020-11-21 23:57

    Without any jQuery:

    function decodeEntities(encodedString) {
      var textArea = document.createElement('textarea');
      textArea.innerHTML = encodedString;
      return textArea.value;
    }
    
    console.log(decodeEntities('1 & 2')); // '1 & 2'

    This works similarly to the accepted answer, but is safe to use with untrusted user input.


    Security issues in similar approaches

    As noted by Mike Samuel, doing this with a

    instead of a