HTML-encoding lost when attribute read from input field

前端 未结 25 3878
时光说笑
时光说笑 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:54

    My pure-JS function:

    /**
     * HTML entities encode
     *
     * @param {string} str Input text
     * @return {string} Filtered text
     */
    function htmlencode (str){
    
      var div = document.createElement('div');
      div.appendChild(document.createTextNode(str));
      return div.innerHTML;
    }
    

    JavaScript HTML Entities Encode & Decode

提交回复
热议问题