简体   繁体   English

在跨度之间插入字符串变量

[英]Inserting an string variable between spans

I want to put my string variable named "bringMe" into this line of code.我想将名为“bringMe”的字符串变量放入这行代码中。

Please help...请帮忙...

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

You can use Template literals .您可以使用模板文字

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.使用 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 .有关更多信息,请参阅此处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如果bringMe是一个变量

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

你可以这样写:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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