HTML-encoding lost when attribute read from input field

前端 未结 25 3812
时光说笑
时光说笑 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 05:01

    function encodeHTML(str) {
        return document.createElement("a").appendChild( 
            document.createTextNode(str)).parentNode.innerHTML;
    };
    
    function decodeHTML(str) {
        var element = document.createElement("a"); 
        element.innerHTML = str;
        return element.textContent;
    };
    var str = "<"
    var enc = encodeHTML(str);
    var dec = decodeHTML(enc);
    console.log("str: " + str, "\nenc: " + enc, "\ndec: " + dec);

提交回复
热议问题