Web APIs (第六天BOM- 操作浏览器)

一、Window对象

1.1 BOM

1.2 定时器-延时函数

 

 1.3 JS 执行机制

 

1.4 location对象

1.4.1href 属性获取完整的 URL 地址,对其赋值时用于地址的跳转

 1.4.2search 属性获取地址中携带的参数,符号 ?后面部分

 1.4.3hash 属性获取地址中的啥希值,符号 # 后面部分

1.4.4reload 方法用来刷新当前页面,传入参数 true 时表示强制刷新 

 1.5 navigator对象

 1.6 histroy对象

二、swiper插件

2.1 插件 

插件: 就是别人写好的一些代码,我们只需要复制对应的代码,就可以直接实现对应的效果

2.1.1学习插件的基本过程

熟悉官网,了解这个插件可以完成什么需求 https://www.swiper.com.cn/

2.1.2看在线演示,找到符合自己需求c的demo https://www.swiper.com.cn/demo/index.html

2.1.3查看基本使用流程 https://www.swiper.com.cn/usage/index.html

2.1.4查看APi文档,去配置自己的插件 https://www.swiper.com.cn/api/index.html

2.1.5注意: 多个swiper同时使用的时候, 类名需要注意区分

三、本地存储

3.1 本地存储特性

3.2 localStorage 

 

3.3 sessionStorage(了解)

四、自定义属性

 

五、案例——动态创建表格(读取本地数据)

遇到的问题:存取本地数据还不太熟练,思考周期太长

 <!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="css/user.css">
</head>

<body>
  <h1>新增学员</h1>
  <div class="info">
    姓名:<input type="text" class="uname">
    年龄:<input type="text" class="age">
    性别: <select name="gender" id="" class="gender">
      <option value="男">男</option>
      <option value="女">女</option>
    </select>
    薪资:<input type="text" class="salary">
    就业城市:<select name="city" id="" class="city">
      <option value="北京">北京</option>
      <option value="上海">上海</option>
      <option value="广州">广州</option>
      <option value="深圳">深圳</option>
      <option value="曹县">曹县</option>

    </select>
    <button class="add">录入</button>
  </div>

  <h1>就业榜</h1>
  <table>
    <thead>
      <tr>
        <th>学号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>薪资</th>
        <th>就业城市</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <!-- <tr>
        <td>1001</td>
        <td>欧阳霸天</td>
        <td>19</td>
        <td>男</td>

        <td>15000</td>
        <td>上海</td>
        <td>
          <a href="javascript:">删除</a>
        </td>
      </tr> -->
    </tbody>
  </table>
  <script>

    // 读取本地存储的数据  封装为函数 
    // 需求①:读取本地存储数据(封装函数)
    //   如果本地存储有数据,则返回 JSON.parse() 之后的对象
    //   如果本地存储没有数据,则默认写入三条数据,注意存储的利用JSON.stringify() 存 储JSON 格式的数据
    function getLocalData() {
      let data = localStorage.getItem('data')
      if (data) {
        //   如果本地存储有数据,则返回 JSON.parse() 之后的对象
        return JSON.parse(data)  //   {xxxxx}
      } else {
        // 如果本地存储没有数据,则默认写入三条数据,注意存储的利用JSON.stringify() 存 储JSON 格式的数据
        let arr = [
          { stuId: 1001, uname: '欧阳霸天', age: 19, gender: '男', salary: '20000', city: '上海' },
          { stuId: 1002, uname: '令狐霸天', age: 29, gender: '男', salary: '30000', city: '北京' },
          { stuId: 1003, uname: '诸葛霸天', age: 39, gender: '男', salary: '2000', city: '北京' },
        ]
        // 写入到本地存储里面
        localStorage.setItem('data', JSON.stringify(arr))
      }
    }
    // 先调用一次
    getLocalData()
    // 获取父元素 tbody
    let tbody = document.querySelector('tbody')
    // 添加数据按钮 
    // 获取录入按钮
    let add = document.querySelector('.add')
    // 获取各个表单的元素
    let uname = document.querySelector('.uname')
    let age = document.querySelector('.age')
    let gender = document.querySelector('.gender')
    let salary = document.querySelector('.salary')
    let city = document.querySelector('.city')
    // 渲染函数  把数组里面的数据渲染到页面中
    function render() {
      // 需求②:渲染模块
      // 先读取本地存储数据,然后渲染
      let arr = getLocalData()

      // 先干掉以前的数据  让tbody 里面原来的tr 都没有
      tbody.innerHTML = ''
      // 在渲染新的数据
      // 根据数据的条数来渲染增加 tr  
      for (let i = 0; i < arr.length; i++) {
        // 1.创建tr  
        let tr = document.createElement('tr')
        // 2.tr 里面放内容
        tr.innerHTML = `
        <td>${arr[i].stuId}</td>
        <td>${arr[i].uname}</td>
        <td>${arr[i].age}</td>
        <td>${arr[i].gender}</td>
        <td>${arr[i].salary}</td>
        <td>${arr[i].city}</td>
        <td>
          <a href="javascript:"  data-id="${i}">删除</a>
        </td>
        `
        // 3.把tr追加给 tobdy  父元素.appendChild(子元素)
        tbody.appendChild(tr)


      }
    }
    // 页面加载就调用函数
    render()

    add.addEventListener('click', function () {
      // 需求③:添加模块
      // 注意,先取的最新的本地存储数据,然后追加
      let arr = getLocalData()
      // 新增了数据,要把新数据存储到本地存储别,忘记转换


      // alert(11)
      // 获得表单里面的值   之后追加给 数组 arr  用 push方法
      arr.push({
        // 得到数组最后一条数据的学号 1003    + 1
        stuId: arr[arr.length - 1].stuId + 1,
        uname: uname.value,
        age: age.value,
        gender: gender.value,
        salary: salary.value,
        city: city.value
      })
      // console.log(arr)

      // 存储到仓库里面 
      localStorage.setItem('data', JSON.stringify(arr))

      // 重新渲染我们的函数
      render()
      // 复原所有的表单数据
      uname.value = age.value = salary.value = ''
      gender.value = '男'
      city.value = '北京'
    })


    // 删除操作, 删除的也是数组里面的数据 , 但是我们用事件委托
    tbody.addEventListener('click', function (e) {
      // 读取本地存储里面的数据
      let arr = getLocalData()
      // alert(11)
      // 我们只能点击了链接 a ,才会执行删除操作
      // 那我们怎么知道你点击了a呢?
      // 俺们只能点击了链接才能做删除操作
      // console.dir(e.target.tagName)
      if (e.target.tagName === 'A') {
        // alert('你点击了链接')
        // 删除操作  删除 数组里面的数据  arr.splice(从哪里开始删,1)
        // 我要得到a的id 需要
        // console.log(e.target.id)
        // console.log(e.target.dataset.id)
        arr.splice(e.target.dataset.id, 1)

        // 存到本地里面
        localStorage.setItem('data', JSON.stringify(arr))

        // 重新渲染我们的函数
        render()
      }
    })
  </script>
</body>

</html>

 css:

* {
  margin: 0;
  padding: 0;
}

a {
  text-decoration: none;
  color:#721c24;
}
h1 {
  text-align: center;
  color:#333;
  margin: 20px 0;
 
}
table {
  margin:0 auto;
  width: 800px;
  border-collapse: collapse;
  color:#004085;
}
th {
  padding: 10px;
  background: #cfe5ff;
  
  font-size: 20px;
  font-weight: 400;
}
td,th {
  border:1px solid #b8daff;
}
td {
  padding:10px;
  color:#666;
  text-align: center;
  font-size: 16px;
}
tbody tr {
  background: #fff;
}
tbody tr:hover {
  background: #e1ecf8;
}
.info {
  width: 900px;
  margin: 50px auto;
  text-align: center;
}
.info  input {
  width: 80px;
  height: 25px;
  outline: none;
  border-radius: 5px;
  border:1px solid #b8daff;
  padding-left: 5px;
}
.info button {
  width: 60px;
  height: 25px;
  background-color: #004085;
  outline: none;
  border: 0;
  color: #fff;
  cursor: pointer;
  border-radius: 5px;
}
.info .age {
  width: 50px;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值