In Javascript, you often encounter string problems, but it will be more troublesome if the string you want to splice too long.
If it is on one line, it will not be readable, but if it is to wrap the line, it will directly report an error.
Here are several Javascript splicing techniques for splicing strings.
Add strings (+)
var items = '<li>' + '<span>Hello world</span>' + '</li>';
Stitching strings with backslash
var items = '<li>' / '<span>Hello world</span>' / '</li>';
Splicing strings with arrays
Use the join method of the array to convert the array into a string.
var empList = ['<li>', '<span>Hello world</span>','</li>'].join("");Based on the array, you can encapsulate a StringBuffer method to complete the string splicing.
function StringBuffer(){ this.buffer = []; } StringBuffer.prototype = { constructor: StringBuffer, append: function(str){ this.buffer.push(str); return this; }, toString: function(){ return this.buffer.join(''); } };ES6 template string
A new type of literal syntax is introduced in ES6, called template strings.
Use an inverse apostrophe` instead of the original single or double quotes.
$('.warning').html(` <h1>Working!</h1> <p>Miscellaneous Restaurant</p> <p>A mere flax thai</p> `);New lines, indents, and spaces in the string will be output to the newly generated string as it is.
If you want to understand the performance issues of string splicing, it is recommended to read Nicholas C. Zakas' book "High-performance Javascript" by Nicholas C. Zakas
The above Javascript string splicing tips (recommended) are all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.