1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| <!DOCTYPE html> <html lang="en">
<head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="../lib/bootstrap.min.css"> <script src="../lib/jquery-3.6.0.js"></script> <style> .comment { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 5px; }
.loading { padding: 10px; border-radius: 25px; z-index: 1; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: auto; height: auto; background-color: rgb(255, 255, 255); font-weight: bold; }
.loading img { width: 50px; animation: load 1s infinite linear; }
@keyframes load { from { left: 0px; }
to { transform: rotate(360deg); } } </style> </head>
<body style="padding: 10px;">
<div class="panel panel-primary"> <div class="panel-heading"> <h3 class="panel-title">添加图书</h3> </div> <div class="panel-body"> <form id="form"> <div>书名</div> <input type="text" id="bookname" class="form-control" name="bookname"> <div>作者</div> <input type="text" id="author" class="form-control" name="author"> <div>出版社</div> <textarea class="form-control" id="publisher" name="publisher"></textarea> <button type="submit" class="btn btn-primary" id="send">发表</button> </form> </div> </div>
<div class="loading"> <img class="load" src="../lib/loading.png" alt="" srcset=""> <span>加载中...</span> </div>
<ul class="list-group">
</ul>
<script> $(function () { setTimeout(book, 1000)
function book() { $.ajax({ url: 'http://ajax-api.itheima.net/api/books', type: 'GET', dataType: 'json', success: function (data) { $('.list-group').empty()
if (data.message === '查询图书列表成功') { data.data.forEach((element, index) => { $('.loading img').attr('src', '../lib/ok.png').css({ animation: 'none' }) $('.loading span').html('加载完成!!!') const comment1 = ` <li class="list-group-item" > <span class="badge" style="background-color: #f0ad4e;">${data.data[index].author}</span> <span class="badge" style="background-color: #5bc0de;">${data.data[index].publisher}</span> <span class="comment">${data.data[index].id + '. ' + data.data[index].bookname}</span> </li> ` $('.list-group').append(comment1); setTimeout(function () { $('.loading').fadeOut() }, 500) }); }
} }) }
$('#form').on('submit', function (e) { e.preventDefault() const addData = $(this).serialize() if (bookname === '' || author === '' || publisher === '') { return alert('请完整填写信息!') } $.ajax({ url: 'http://ajax-api.itheima.net/api/books', type: 'POST', data: addData, dataType: 'json', success: function (data) { book() $('#form')[0].reset() } }) }) }) </script> </body>
</html>
|