script tag prevents template content from being displayed on the page. Therefore, when using jQuery to render data, there is no need for HTML string concatenation. Instead, you only need to directly take out the HTML text in the template and use JS to do regular replacement:
var template = document.getElementById("my-template").innerHTML;
var html = template
.replace(/%name%/, data['name'])
.replace(/%value%/, data['value']);
// insert HTML...
In this way, you can easily render the JSON data obtained by Ajax into styled HTML text.
This is a common HTML template in the jQuery era. The code looks like the following
Thescript tag prevents template content from being displayed on the page. Therefore, when using jQuery to render data, there is no need for HTML string concatenation. Instead, you only need to directly take out the HTML text in the template and use JS to do regular replacement:
In this way, you can easily render the JSON data obtained by Ajax into styled HTML text.