6_字符串扩展.html 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .active {
  9. color: red;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div class="box"></div>
  15. <script>
  16. var oBox = document.querySelector(".box");
  17. // var oH1 = document.createElement("h1");
  18. // oH1.classList.add("active");
  19. // oH1.innerHTML = "hello world";
  20. // oBox.append(oH1);
  21. // var str = "hello world";
  22. // oBox.innerHTML = "<h1 class='active'>"+str+"</h1>";
  23. // oBox.innerHTML = `<h1 class="active">
  24. // ${str}
  25. // </h1>`
  26. // 模板字符串 ``
  27. // 1. 可以换行
  28. // 2. 可以在字符串中使用变量 ${变量名}
  29. // 3. 内部不区分单引号和双引号
  30. let str = "username=张三;";
  31. let str2 = "hello";
  32. let str3 = " world ";
  33. // startsWith 检查字符串是否以指定的字符串开头
  34. // console.log(str.startsWith("username"));
  35. // endsWith 检查字符串是否以指定的字符串结尾
  36. // console.log(str.endsWith(";"));
  37. // repeat 重复字符串指定次数
  38. // console.log(str.repeat(2));
  39. // padStart 字符串补全指定长度 从左侧开始补全
  40. // console.log(str2.padStart(10,"*"));
  41. // padEnd 字符串补全指定长度 从右侧开始补全
  42. // console.log(str2.padEnd(10,"*"));
  43. console.log(str3);
  44. // trim 去除字符串首尾空格
  45. // console.log(str3.trim());
  46. //trimStart 去除字符串左侧空格
  47. // console.log(str3.trimStart());
  48. // trimEnd 去除字符串右侧空格
  49. // console.log(str3.trimEnd());
  50. console.log(str2.at(0));
  51. </script>
  52. </body>
  53. </html>