index.js

  1. var baseUrl ="http://192.168.4.18:3000/"
  2. function ajax({
  3. url,
  4. method = "get",
  5. success
  6. }){
  7. var xhr = new XMLHttpRequest();
  8. xhr.open(method,url=baseUrl+url,true);
  9. xhr.send();
  10. xhr.onreadystatechange = function(){
  11. if(xhr.readyState == 4 && xhr.status == 200){
  12. var result = JSON.parse(xhr.responseText);
  13. success(result)
  14. }
  15. }
  16. }

search.js

  1. window.onload = function () {
  2. $("#btn").click(function () {
  3. $(".container").html(""); //内容为空
  4. var value = $("#input").val();
  5. var url = `search?keywords=${value}&type=10`;
  6. ajax({
  7. url,
  8. success: res => {
  9. handleData(res)
  10. }
  11. })
  12. })
  13. function handleData(res) {
  14. var albums = res.result.albums
  15. albums.forEach(item => {
  16. var template = `
  17. <div>
  18. <img src=${item.blurPicUrl}>
  19. <a href="#" class="title">${item.name}</a>
  20. <p class="name">${item.artist.name}</p>
  21. </div>
  22. `
  23. $(".container").append(template)
  24. })
  25. }
  26. }

html


<!DOCTYPE html>
<html lang="en">
<head>
    <meta name="referrer" content="no-referrer">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="http://at.alicdn.com/t/font_2158967_dxgxhdffcnb.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <script src="http/index.js"></script>
    <script src="http/search.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #app{
            padding-top: 50px;
        }
        .search{
            width: 400px;
            height: 40px;
            border: 1px solid #999;
            margin:0 auto;
            border-radius: 10px;
            overflow: hidden;
            position: relative;
        }
        .search>input,.search>button{
            position: absolute;
            top: 0;
            outline: none;
            border: none;
        }
        .search>input{
            width: 350px;
            height: 40px;
            left: 0;
            padding-left: 15px;
        }
        .search>button{
            width: 50px;
            height: 40px;
            right: 0;
            border-left: 1px solid #666;
        }
        .search>button .icon-sousuo_huaban{
            font-size: 25px;
            color: #666;
        }
        .result{
            color: #666;
            margin-top: 30px;
            margin-left: 276px;
            font-size: 13px;
            margin-bottom: 20px;
        }
        .red{
            color: red;
        }
        .container{
            width: 800px;
            margin: 0 auto;
            display: grid;
            grid-template-columns: repeat(5,140px);
            grid-gap: 20px 30px;
        }
        .container img{
            width: 140px;
            height: 140px;
        }
        .title{
            text-decoration: none;
            color: black;
            font-size: 14px;
        }
        .title:hover{
            text-decoration: underline;
        }
        .name{
            font-size: 12px;
            color: #666;
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="search">
            <input type="text" value="你" id="input">
            <button id="btn"><i class="iconfont icon-sousuo_huaban"></i></button>
        </div>
        <div class="container">   //渲染

        </div>
    </div>
</body>
</html>