diff --git "a/29 \350\267\257\347\216\262/20231214 \345\244\247\345\244\215\344\271\240.md" "b/29 \350\267\257\347\216\262/20231214 \345\244\247\345\244\215\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..f3da47be696f3ef3895a899bc7063487a55d6767 --- /dev/null +++ "b/29 \350\267\257\347\216\262/20231214 \345\244\247\345\244\215\344\271\240.md" @@ -0,0 +1,276 @@ +tab栏切换 + +```html + + +``` + +轮播图 + +```html + + +``` + +图书管理 + +```js + +const creator = '小鹿666' + +function getBookListAndShow(){ + + //获取数据 + $.ajax({ + url:'https://hmajax.itheima.net/api/books', + type:'get', + data:{ + creator + + },success: (result) => { + //get和post都要data + const data = (result.data); +console.log(result.data); + //渲染数据 + const str = $.map(data,(item,i) => { + + return ` + + ${i+1} + ${item.bookname} + ${item.author} + ${item.publisher} + + 删除 + 编辑 + + + ` + }).join('') + + $('.list').html(str); + } + }) +} + +getBookListAndShow() + + //添加按钮 + const modal = new bootstrap.Modal($('.add-modal')); + const form = $('add-form'); + $('.add-btn').bind('click',() => { + const inputData = {} + inputData.bookname = $('.bookname').val(); + inputData.author = $('.author').val(); + inputData.publisher = $('.publisher').val(); + + $.ajax({ + + url:'https://hmajax.itheima.net/api/books', + type:'post', + data:{ + ...inputData, + creator + },success:(result) => { + + getBookListAndShow(); + + modal.hide() + + } + + }) + + }) + + //删除 + $('.list').on('click','.del', function (){ + + //确认一次是否真的删除 + if (confirm('是否真的要删除?')) { + + const id = $(this).parent().data('id'); + $.ajax( + { + url:`https://hmajax.itheima.net/api/books/${id}`, + type:'delete', + success:() => { + getBookListAndShow() + } + } + ) + } + + }) + + + //编辑 + $('.list').on('click','.edit',function(){ + + const modal = new bootstrap.Modal($('.edit-modal')); + const id = $(this).parent().data('id'); + + //通过接口,获取数据回显到表单 + $.ajax({ + url:`https://hmajax.itheima.net/api/books/${id}`, + type:'get', + success:result => { + + const {id,bookname,author,publisher } = result.data; + $('.edit-from .bookname').val(bookname); + $('.edit-from .id').val(id); + $('.edit-from .author').val(author); + $('.edit-from .publisher').val(publisher); + } + }) + + modal.show() + + $('.edit-btn').bind('click',() => { + + // const form = $('edit-form'); + // const inputData = {} + // inputData.bookname = $('edit-form .bookname').val(); + // inputData.author = $('edit-form .author').val(); + // inputData.publisher = $('edit-form .publisher').val(); + // inputData.id = $('edit-form .id').val(); + + const inputData = serialize($('.edit-form')[0],{hash:true,empty:true}) + $.ajax({ + + url:`https://hmajax.itheima.net/api/books/${inputData.id}`, + type:'PUT', + data:{ + ...inputData, + creator + },success:(result) => { + + getBookListAndShow(); + + modal.hide() + + } + }) + }) + + }) + + + + + + + + + + + + + + +``` +