Unescape HTML entities in Javascript?

前端 未结 30 2923
野趣味
野趣味 2020-11-21 05:40

I have some Javascript code that communicates with an XML-RPC backend. The XML-RPC returns strings of the form:


30条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 05:50

    Closures can avoid creating unnecessary objects.

    const decodingHandler = (() => {
      const element = document.createElement('div');
      return text => {
        element.innerHTML = text;
        return element.textContent;
      };
    })();
    

    A more concise way

    const decodingHandler = (() => {
      const element = document.createElement('div');
      return text => ((element.innerHTML = text), element.textContent);
    })();
    

提交回复
热议问题