1. 1.作用:简化字符串的拼接;
  2. 2.使用:
  3. *将拼接的字符串内容必须放在` `里面
  4. *字符串中需要变化的部分,放在${ xxx }中。

作用:可以在字符串中使用变量

  1. var str = "hello";
  2. var age = 18;
  3. console.log('world'+str);
  4. /*
  5. `` 字符串模板 可以在字符串中插入变量${}
  6. */
  7. var sum = `world${str}`;
  8. console.log(sum);

应用示例:

  1. <!-- <div>
  2. <p>name</p>
  3. <p>age</p>
  4. </div>
  5. <div>
  6. <p>name</p>
  7. <p>age</p>
  8. </div> -->
  9. <script>
  10. var arr = [
  11. {
  12. imgUrl: 'img/1.jpg',
  13. name: 'cheng',
  14. age: 18
  15. },
  16. {
  17. imgUrl: 'img/2.jpg',
  18. name: 'lis',
  19. age: 18
  20. }
  21. ]
  22. for (var i = 0; i < arr.length; i++) {
  23. var { imgUrl, name, age } = arr[i];
  24. var template = `
  25. <div>
  26. <p>${name}</p>
  27. <p>${age}</p>
  28. <img src='${imgUrl}'>
  29. </div>
  30. `
  31. $('body').prepend(template)
  32. }
  33. </script>

4-1 ajxa网易云实例

  1. *{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. .container{
  6. width: 1000px;
  7. border: 1px solid #333;
  8. padding: 20px;
  9. margin-left: auto;
  10. margin-right: auto;
  11. display: flex;
  12. flex-wrap: wrap;
  13. justify-content: space-between;
  14. }
  15. .container .item{
  16. padding: 5px;
  17. width: 140px;
  18. border: 1px solid #333;
  19. overflow: hidden;
  20. border-radius: 10%;
  21. }
  22. .item img{
  23. width: 100%;
  24. }
  25. .item{
  26. display: flex;
  27. flex-direction: column;
  28. justify-content: space-between;
  29. position: relative;
  30. }
  31. .size{
  32. width: 100%;
  33. font-size: 12px;
  34. color: #666;
  35. overflow: hidden;
  36. white-space: nowrap;
  37. text-overflow: ellipsis;
  38. }
  39. .font{
  40. font-size: 14px;
  41. overflow: hidden;
  42. white-space: nowrap;
  43. text-overflow: ellipsis;
  44. }
  45. .bj{
  46. position: absolute;
  47. bottom: 40px;
  48. width: 100%;
  49. height: 27px;
  50. background-image: url(../images/coverall.png);
  51. background-position: 0 -537px;
  52. color: #ccc;
  53. font-size: 12px;
  54. line-height: 27px;
  55. }
  56. #dw{
  57. position: absolute;
  58. font-size: 25px;
  59. right: 10px;
  60. }
  61. <div class="container">
  62. <div class="item">
  63. <img src="" alt="">
  64. <p></p>
  65. <p></p>
  66. <p><span></span> <span></span></p>
  67. </div>
  68. <script>
  69. var url="http://47.108.197.28:3000/top/playlist?limit=";
  70. $.ajax({
  71. url,
  72. success:res=>{
  73. var playlists= res.playlists;
  74. for(var i=0;i<playlists.length;i++){
  75. var {coverImgUrl,name,creator,playCount}=playlists[i];
  76. var nickname=creator.nickname
  77. var count=Math.round(playCount/10000)+"万";
  78. console.log(count);
  79. var temp=`
  80. <div class="item">
  81. <img src="${coverImgUrl}" />
  82. <p class="bj">${count}<span class="iconfont">&#xea10;</span><span class="iconfont" id="dw">&#xe66d;</span></p>
  83. <p class="font">${name}</p>
  84. <p class="size"><span>by</span> ${nickname}</p>
  85. </div>
  86. `
  87. $(".container").prepend(temp);
  88. }
  89. }
  90. })

image.png