1.通过a标签实现页面的跳转

index

  1. `<!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="ajax/index.js"></script>
  9. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
  10. <style>
  11. .item{
  12. border:1px solid #333;
  13. }
  14. .item img{
  15. width:100px;
  16. height:100px
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="app">
  22. </div>
  23. <script>
  24. var url = 'http://47.108.197.28:3000/top/playlist?cat=华语';
  25. $ajax({
  26. url,
  27. success:res=>{
  28. console.log(res.playlists)
  29. var playlists = res.playlists;
  30. playlists.forEach((item,index)=>{
  31. var {name,id,coverImgUrl} = item;
  32. var template = `
  33. <a href='detail.html?id=${id}'>
  34. <div class='item'>
  35. <img src='${coverImgUrl}'/>
  36. <p>${name}</p>
  37. </div>
  38. </a>
  39. `
  40. $("#app").append(template)
  41. })
  42. }
  43. })
  44. </script>
  45. </body>
  46. </html>

detail

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="ajax/index.js"></script>
  9. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
  10. <style>
  11. img{
  12. width:200px;
  13. height: 200px;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="app">
  19. </div>
  20. <script>
  21. /* 1、获取id值 */
  22. var id = location.search.split("=")[1];
  23. // console.log(id)
  24. /* 2、根据id值发送http请求 */
  25. var url = `http://47.108.197.28:3000/playlist/detail?id=${id}`;
  26. $ajax({
  27. url,
  28. success:res=>{
  29. console.log(res.playlist)
  30. var {name,coverImgUrl,description} = res.playlist;
  31. var template = `
  32. <div>
  33. <img src='${coverImgUrl}'/>
  34. <p>${name}</p>
  35. <P>${description}</p>
  36. </div>
  37. `
  38. $("#app").append(template)
  39. }
  40. })
  41. </script>
  42. </body>
  43. </html>

2.通过点击事件跳转

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="ajax/index.js"></script>
  9. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
  10. <style>
  11. .item{
  12. border:1px solid #333;
  13. }
  14. .item img{
  15. width:100px;
  16. height:100px
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="app">
  22. </div>
  23. <script>
  24. var url = 'http://47.108.197.28:3000/top/playlist?cat=华语';
  25. $ajax({
  26. url,
  27. success:res=>{
  28. var playlists = res.playlists;
  29. playlists.forEach((item,index)=>{
  30. var {name,id,coverImgUrl} = item;
  31. var template = `
  32. <div class='item' data-aid=${id}>
  33. <img src='${coverImgUrl}'/>
  34. <p>${name}</p>
  35. </div>
  36. `
  37. $("#app").append(template)
  38. })
  39. $(".item").click(function(event){
  40. var {aid} = event.currentTarget.dataset;
  41. console.log(aid)
  42. location.href = `detail.html?id=${aid}`
  43. })
  44. }
  45. })
  46. </script>
  47. </body>
  48. </html>