简体   繁体   中英

Inserting an string variable between spans

I want to put my string variable named "bringMe" into this line of code.

Please help...

var filler = "<span><font color='#FF0000'>***** bringMe Variable *****</font></span>";

You can use .

Template literals : Template literals are string literals allowing embedded expressions

 let bringMe = "My variable" let filler = `<span><font color='#FF0000'>${bringMe}</font></span>`; console.log(filler);

Use template literals as shown by Maheer. To expand, the difference is using the backtick "`" instead of the standard single/double quotation marks ("/').

Using template literals allows you to embed expressions and have multi-line strings.

For more information, see here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals .

Use + operator and "" to add (concatenate )strings

 var filler = "<span><font color='#FF0000'>" +'bringMe'+ " Variable *****</font></span>"; console.log(filler)

If bringMe is a variable

 var bringMe="string" var filler = "<span><font color='#FF0000'>" +bringMe+ " Variable *****</font></span>"; console.log(filler)

你可以这样写:

`<span><font color='#FF0000'>${bringMe}</font></span>`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM