HTML text escape tips

Time:2021-12-5

Today in CSDN to see a HTML text escape tips, very simple.

1. Use the function of DOM. First pass it to a DOM object as innerText, and then take the innerHTML attribute to get the escaped text.

For example:

div1.innerText = “<h1>title title….</h1>”;

var str = div1.innerHTML; // “&lt;h1&gt;title title…”

2. Assuming that there is a text context, you can perform $(x). Text (context). Html() on a jQuery object $(x), and an escaped text will be returned.

var str = $(x).text(context).html()