HTML-encoding lost when attribute read from input field

前端 未结 25 3916
时光说笑
时光说笑 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 04:53

    I know this is an old one, but I wanted to post a variation of the accepted answer that will work in IE without removing lines:

    function multiLineHtmlEncode(value) {
        var lines = value.split(/\r\n|\r|\n/);
        for (var i = 0; i < lines.length; i++) {
            lines[i] = htmlEncode(lines[i]);
        }
        return lines.join('\r\n');
    }
    
    function htmlEncode(value) {
        return $('
    ').text(value).html(); }

提交回复
热议问题