HTML-encoding lost when attribute read from input field

前端 未结 25 3844
时光说笑
时光说笑 2020-11-21 04:04

I’m using JavaScript to pull a value out from a hidden field and display it in a textbox. The value in the hidden field is encoded.

For example,



        
25条回答
  •  天命终不由人
    2020-11-21 04:51

    As far as I know there isn't any straight forward HTML Encode/Decode method in javascript.

    However, what you can do, is to use JS to create an arbitrary element, set its inner text, then read it using innerHTML.

    Let's say, with jQuery, this should work:

    var helper = $('chalk & cheese').hide().appendTo('body');
    var htmled = helper.html();
    helper.remove();
    

    Or something along these lines.

提交回复
热议问题