How to decode HTML entities using jQuery?

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

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

19条回答
  •  醉梦人生
    2020-11-22 00:07

    Try this :

    var htmlEntities = "<script>alert('hello');</script>";
    var htmlDecode =$.parseHTML(htmlEntities)[0]['wholeText'];
    console.log(htmlDecode);

    parseHTML is a Function in Jquery library and it will return an array that includes some details about the given String..

    in some cases the String is being big, so the function will separate the content to many indexes..

    and to get all the indexes data you should go to any index, then access to the index called "wholeText".

    I chose index 0 because it's will work in all cases (small String or big string).

提交回复
热议问题