1. <style>
    2. input{
    3. width:250px;
    4. height: 35px;
    5. border-radius: 30px;
    6. border:1px solid #333;
    7. outline: none;
    8. padding-left: 20px;
    9. }
    10. img{
    11. width:250px;
    12. }
    13. </style>
    14. <script src="lib/jquery-3.4.1.js"></script>
    15. </head>
    16. <body>
    17. <input type="text" id="input" placeholder="请搜索">
    18. <div>
    19. <img src="" alt=""> <br>
    20. <audio src="" controls="controls" autoplay></audio>
    21. </div>
    22. <script>
    23. var url = "https://music.aityp.com/";
    24. var input = document.getElementById("input");
    25. input.onkeydown = function(event){
    26. if(event.keyCode == 13){
    27. console.log(this.value)
    28. /* 1.获取id */
    29. $.ajax({
    30. url:`${url}search?keywords=${this.value}`,
    31. type:'get',
    32. dataType:"json",
    33. success:res=>{
    34. var id = res.result.songs[0].id;
    35. /* 2.获取imgUrl */
    36. $.ajax({
    37. url:`${url}song/detail?ids=${id}`,
    38. type:"get",
    39. dataType:"json",
    40. success:res=>{
    41. var imgUrl = res.songs[0].al.picUrl;
    42. $("img").attr("src",imgUrl)
    43. }
    44. })
    45. /* 3.获取音乐url */
    46. $.ajax({
    47. url:`${url}song/url?id=${id}`,
    48. type:"get",
    49. dataType:"json",
    50. success:res=>{
    51. var musicUrl = res.data[0].url;
    52. $("audio").attr("src",musicUrl)
    53. }
    54. })
    55. }
    56. })
    57. }
    58. }
    59. </script>

    test.gif