JavaScript字符串的拼接和转义字符
这篇博文主要讲一下字符串的拼接和转义字符的简单使用。
1.字符串的拼接
// js字符串的拼接 +
var str="hello";
var str1="world";
console.log(str+str1);
// var str=``;//模板字符串
console.log(`${str}${str1}`);
// 外单内双 外双内单
var str3='"';
var str4="'";
console.log(str3);
document.write("<h2 style='color:red'>你好</h2>");
document.write("<h2 style="+"color:red"+">你好</h2>");
2.转义字符
html代码:
<textarea name="" id="" cols="30" rows="10">
你好 中国
</textarea>
<textarea name="" id="text" cols="30" rows="10"></textarea>
js代码:
// 转义字符 \ 使用\ 可以插入特殊符号
document.write("<h2 style=\"color:red\">你好</h2>");
// \' 单引号
// \" 双引号
// \\ 斜杆
// \n 换行
// \r 回车
// \t tab
// \b 空格
// \f 换页
console.log("hello \n world");
document.getElementById("text").value="hello \n China";
视频讲解链接:
https://www.bilibili.com/video/BV1rK4y1t78H/