js dynamically generates an instance of html trigger event parametric character escape

  • 2021-07-21 06:49:28
  • OfStack

Usually, in the process of dynamically generating html by using JS, corresponding attribute elements such as styles and events will be embedded. At this time, the so-called "single and double quotation marks are not enough" often occurs. Don't worry, at this time, escape characters in html language can be used to solve it.

Let's introduce the corresponding escape characters under 1:

& -(ampersand) Escape characters correspond to & amp;

"--(double quote) Double quotation marks with escape characters corresponding to & quot;

'--(single quote) single quotation marks with escape characters corresponding to & #039;

< --(less than) Less than sign, the escape character corresponds to & lt;

> -(greater than) greater than sign, the escape character corresponds to & gt;

For example, in js:

var html = " < div onclick='orderIt( & #039"+food.picture+" & #039)' > Click on it < /div > ";

Then the corresponding generated html is:

< div onclick = "orderIt ('price')" > Click on it < /div >


Related articles: