How to decode HTML entities using jQuery?

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

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

19条回答
  •  耶瑟儿~
    2020-11-22 00:03

    To decode HTML Entities with jQuery, just use this function:

    function html_entity_decode(txt){
        var randomID = Math.floor((Math.random()*100000)+1);
        $('body').append('
    '); $('#random'+randomID).html(txt); var entity_decoded = $('#random'+randomID).html(); $('#random'+randomID).remove(); return entity_decoded; }

    How to use:

    Javascript:

    var txtEncoded = "á é í ó ú";
    $('#some-id').val(html_entity_decode(txtEncoded));
    

    HTML:

    
    

提交回复
热议问题