人肉dom+ajax

一、json-server的安装

npm install -g json-server

二、db.json

{
  "books": [
    {
      "id": 1,
      "name": "三体",
      "price": 23.5
    },
    {
      "id": 2,
      "name": "流浪地球",
      "price": 100
    },
    {
      "name": "某书",
      "price": "23.5",
      "id": 3
    },
    {
      "name": "某书",
      "price": "23.5",
      "id": 4
    },
    {
      "name": "某书",
      "price": "23.5",
      "id": 5
    },
    {
      "name": "某书",
      "price": "23.5",
      "id": 6
    },
    {
      "name": "某书",
      "price": "23.5",
      "id": 7
    },
    {
      "name": "某书",
      "price": "23.5",
      "id": 8
    },
    {
      "name": "某书",
      "price": "23.5",
      "id": 9
    }
  ]
}

三、index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #books table{
            width: 600px;
            border: 1px solid blue;
            border-collapse: collapse;
        }
        #books th,#books td{
            border: 1px solid blue;
        }
    </style>
</head>
<body>

    <form id="addbook" action="#">
        <input type="text" name="bname" value="某书">
        <input type="number" name="price" step="0.5" min="0" value="23.5">
        <button>添加</button>
    </form>
    <div id="books">

    </div>
    <script>
        let url="http://localhost:3000/books"
        let id=1;
        let data=[]
            //[{id:id++,name:'三体',price:234.50},{id:id++,name:'流浪地球',price:34.50}];
        let books=document.querySelector('#books')
        //添加表单的提交方法
        document.querySelector('#addbook').addEventListener('submit',function (event) {
            let book={name:this.bname.value,price:this.price.value};
            fetch(url,
                {
                    method:"POST",body:JSON.stringify(book),
                    headers:{"Content-Type":"application/json"}
                })
                .then(function (res) {
                    return res.json()
                })
                .then(function (book) {
                    data.push(book)
                    render()
                    event.preventDefault()
                })
        })
        function render(){
            books.innerHTML=""
            var table=document.createElement('table')
            data.forEach(function (book) {
                let tr=document.createElement('tr');
                tr.appendChild(document.createElement('td')).innerText=book.id
                tr.appendChild(document.createElement('td')).innerText=book.name
                tr.appendChild(document.createElement('td')).innerText=book.price
                //加一个button
                let btn=document.createElement("button")
                btn.innerText="删除"
                tr.appendChild(document.createElement('td')).appendChild(btn)
                //给删除按钮添加事件监听
                btn.addEventListener('click',function () {
                    let index=data.findIndex(function (bk) {
                        return bk.id==book.id
                    })
                    data.splice(index,1)
                    render()
                })
                table.appendChild(tr)
            })
            books.appendChild(table)
        }
        window.onload=function(){
            fetch(url,{method:"GET"})
                .then(function (res) {
                    return res.json();
                })
                .then(function (books) {
                    data=books
                    render()
                })


        }
    </script>
</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,337评论 0 3
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,748评论 1 45
  • 一、基本认知 一个在前端本地运行,可以存储json数据的server。 二、基本使用方式(两种) 1.全局使用 (...
    南慕瑶阅读 45,928评论 0 25
  • 今天突然想到一个问题,如果上帝送你一样礼物,只能在两者当中选一个,你会选什么?一个是会游泳(达到奥运冠军...
    梁增伙阅读 373评论 0 2
  • 今天戴老师给我们留的作业还是回家说三件事…(第一件事,让我们查外国人早饭吃啥?第二件事让我们回家编两道数学题,因为...
    昊昊的每一天阅读 326评论 0 0