Manjushaka / blog

3 stars 0 forks source link

html encoding #17

Open Manjushaka opened 7 years ago

Manjushaka commented 7 years ago
  1. hmtl 字符编码
    
    function htmlEncode(value){
    // Create a in-memory div, set its inner text (which jQuery automatically encodes)
    // Then grab the encoded contents back out. The div never exists on the page.
    return $('<div/>').text(value).html();
    }

function htmlDecode(value){ return $('

').html(value).text(); }

如:
    if (this.input) {
        jsonData.fText = $('<div />').text(jsonData.text).html().replace(new RegExp($('<div />').text(this.input).html(), 'g'), `<em>${this.input}</em>`);
    } else {
        jsonData.fText = $('<div />').text(jsonData.text).html();
    }