dom阶段实战内容

news2025/5/10 2:14:41

window定时器方法

◼ 目前有两种方式可以实现:
 setTimeout 允许我们将函数推迟到一段时间间隔之后再执行。
 setInterval 允许我们重复运行一个函数,从一段时间间隔之后开始运行,之后以该时间间隔连续重复运行该函数。
◼ 并且通常情况下有提供对应的取消方法:
 clearTimeout:取消setTimeout的定时器;
 clearInterval:取消setInterval的定时器;
◼ 大多数运行环境都有内置的调度程序,并且提供了这些方法:
 目前来讲,所有浏览器以及 Node.js 都支持这两个方法;
 

setTimeout(foo, 3000)注意此处foo是没有小括号的,如果有括号表示把函数的返回值放进去了

setTimeout的使用

setTimeout的语法如下:
 func|code:想要执行的函数或代码字符串。
➢ 一般传入的都是函数,由于某些历史原因,支持传入代码字符串,但是不建议这样做;
 delay:执行前的延时,以毫秒为单位(1000 毫秒 = 1 秒),默认值是 0;
 arg1,arg2...:要传入被执行函数(或代码字符串)的参数列表;
◼ clearTimeout方法:
 setTimeout 在调用时会返回一个“定时器标识符(timer identifier)”,我们可以使用它来取消执行。 

  <button class="out">取消setTimeout定时器</button>  
  var timeoutID = setTimeout(foo, 3000)
 
    var timeoutBtn = document.querySelector(".out")
    timeoutBtn.onclick = function() {
      // 点击设置的按钮取消调度
      clearTimeout(timeoutID)
    }

setInterval的使用

setInterval也会返回一个“定时器标识符(timer identifier)”,我们可以通过clearInterval来取消这个定时器。

     var itvID = setInterval(bar, 3000)
 
     var itvBtn = document.querySelector(".itv")
     itvBtn.onclick = function() {
       clearInterval(itvID)
     }
    setInterval(function() {

    }, 3000)

 

没懂

案例实战1-消息滚动切换

易错:如果下面这么写,在函数内部定义数组索引值为.,则每次重复调用函数只会实现从0-1的切换

    // 2.2.3s切换一次数据
    setInterval(function() {
  var currentIndex = 0 // 记录当前展示到的索引位置
      // 1> 根据索引获取item
      var tipItem = tipList[currentIndex]
 
      // 2> 给DOM设置内容
      imgEl.src = tipItem.icon
      spanEl.textContent = tipItem.title
    }, 3000)

正确做法是:

math.random随机得到0<=x<1的数值,向下取整,得到索引值0-2

    // 随机
     Math.floor(Math.random() * tipList.length)
  <style>
    .tip-bar {
      display: inline-flex;
      align-items: center;
      height: 30px;
      background-color: rgba(0,0,0,.4);
      border-radius: 16px;
    }
    img {
      width: 30px;
      height: 30px;
      border-radius: 50%;
      margin-right: 5px;
    }
    span {
      font-size: 13px;
      color: white;
      margin-right: 8px;
    }
  </style>
</head>
<body>
   
   
  <div class="tip-bar">
    <img src="https://bfs.biyao.com/group1/M01/A2/67/rBACVGA_iOuAYaTxAAAPbted3yE165.png" alt="">
    <span>183***138对这件商品感兴趣</span>
  </div>
 
  <script>
    // 1.从服务器拿到数据ajax/fetch请求
    let tipList = [
      {
        icon: 'https://bfs.biyao.com/group1/M01/A6/97/rBACYWBCHqyAFH5tAAANZXX5Eww646.png',
        title: 'coderwhy对这件商品感兴趣'
      },
      {
        icon: 'https://bfs.biyao.com/group1/M01/A2/67/rBACVGA_iOuAYaTxAAAPbted3yE165.png',
        title: '123***814对这件商品感兴趣'
      },
      {
        icon: 'https://bfs.biyao.com/group1/M00/7F/4E/rBACYV16HseAP-PnAAAW9bbVoKE463.png',
        title: '刘军对这件商品感兴趣'
      }
    ]
 
    // 2.动态的切换数据
    // 2.1.获取元素
    var tipBar = document.querySelector(".tip-bar")
    var imgEl = tipBar.querySelector("img")
    var spanEl = tipBar.querySelector("span")
 
    // 2.2.3s切换一次数据
    var currentIndex = 0 // 记录当前展示到的索引位置
    setInterval(function() {
      // 1> 根据索引获取item
      var tipItem = tipList[currentIndex]
 
      // 2> 给DOM设置内容
      imgEl.src = tipItem.icon
      spanEl.textContent = tipItem.title
       
      // 3> 重新计算索引
      currentIndex++
      if (currentIndex === tipList.length) {
        currentIndex = 0
      }
    }, 3000)
 
    // 随机则不需要索引值递增
    // Math.floor(Math.random() * tipList.length)
 
  </script>

案例实战2-m站头部关闭

实现慢慢消失的效果:????

 transition: all .5s ease-out;
topBar.style.height = 0
  <script>
    // 1.获取元素
    var topBar = document.querySelector(".top-bar")
    var deleteEl = topBar.querySelector(".delete")
 
    // 2.监听delete的点击
    deleteEl.onclick = function() {
      topBar.style.height = 0
      // setTimeout(function() {
      //   topBar.remove()
      // }, 300)
    }
 
    // 3.监听过渡动画结束
    topBar.ontransitionend = function() {
      topBar.remove()
    }
 
  </script>
<!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>
   
  <style>
    .top-bar {
      display: flex;
      flex-direction: row;
      align-items: center;
      height: 45px;
      width: 375px;
      background-color: black;
      /* 关键 */
      overflow: hidden;
      transition: all .5s ease-out;
    }
    .delete {
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
      height: 100%;
      width: 30px;
      cursor: pointer;
    }
    .delete img {
      height: 10px;
      width: 10px;
    }
    .logo {
      height: 30px;
      width: 30px;
      margin-left:3px;
      margin-right: 30px;
      cursor: pointer;
    }
    span {
      color: white;
      font-size: 14px;
      flex: 1;
 
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .btn {
      width: 94px;
      height: 100%;
      line-height: 45px;
      text-align: center;
      font-size: 14px;
      color: #fff;
      background-color: #F63515;
    }
  </style>
</head>
<body>
   
  <div class="top-bar">
    <div class="delete">
      <img src="./img/delete.png" alt="">
    </div>
    <img class="logo" src="./img/logo.png" alt="">
    <span>打开京东App,购物更轻松</span>
    <div class="btn">立即打开</div>
  </div>
 
  <script>
    // 1.获取元素
    var topBar = document.querySelector(".top-bar")
    var deleteEl = topBar.querySelector(".delete")
 
    // 2.监听delete的点击
    deleteEl.onclick = function() {
      topBar.style.height = 0
      // setTimeout(function() {
      //   topBar.remove()
      // }, 300)
    }
 
    // 3.监听过渡动画结束
    topBar.ontransitionend = function() {
      topBar.remove()
    }
 
  </script>
 
</body>
</html>

案例实战3-侧边栏显示

iconEl.style.backgroundPosition = `-48px -${50*i}px`
    // 1.动态给icon设置backgroundPosition
    var iconEls = document.querySelectorAll(".icon")
    for (var i = 0; i < iconEls.length; i++) {
      var iconEl = iconEls[i]
      iconEl.style.backgroundPosition = `-48px -${50*i}px`
    }
 
    // 2.实现鼠标进入动画
    // 方案一: mouseenter(不能使用事件委托)
    var itemEls = document.querySelectorAll(".item")
    for (var itemEl of itemEls) {
      itemEl.onmouseenter = function() {
        var nameEl = this.children[1]
        nameEl.style.width = "62px"
      }
      itemEl.onmouseleave = function() {
        var nameEl = this.children[1]
        nameEl.style.width = "0"
      }
    }
<!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>
  <style>
    .tool-bar {
      position: fixed;
      top: 30%;
      right: 0;
 
      display: flex;
      flex-direction: column;
      align-items: center;
 
      width: 35px;
    }
 
    .item {
      position: relative;
      width: 35px;
      height: 35px;
      margin-bottom: 1px;
       
      background-color: #7a6e6e;
      border-radius: 3px 0 0 3px;
    }
 
    .icon {
      display: inline-block;
      width: 100%;
      height: 100%;
      cursor: pointer;
      background-image: url(./img/toolbars.png);
    }
 
    /* .icon01 {
      background-position: -48px 0;
    }
    .icon02 {
      background-position: -48px -50px;
    }
    .icon03 {
      background-position: -48px -100px;
    }
    .icon04 {
      background-position: -48px -150px;
    } */
 
    .name {
      position: absolute;
      z-index: -1;
      right: 35px;
      /* left: -62px; */
      top: 0;
       
      width: 0;
      height: 35px;
      line-height: 35px;
 
      color: #fff;
      text-align: center;
      font-size: 12px;
      background-color: #7a6e6e;
      cursor: pointer;
 
      border-radius: 3px 0 0 3px;
      transition: width .2s ease;
    }
 
    .item:hover,
    .item:hover .name {
      background-color: #cd1926;
    }
  </style>
</head>
<body>
   
  <div class="tool-bar">
    <div class="item">
      <i class="icon icon01"></i>
      <div class="name">购物车</div>
    </div>
    <div class="item">
      <i class="icon icon02"></i>
      <div class="name">收藏</div>
    </div>
    <div class="item">
      <i class="icon icon03"></i>
      <div class="name">限时活动</div>
    </div>
    <div class="item">
      <i class="icon icon04"></i>
      <div class="name">大礼包</div>
    </div>
  </div>
 
  <script>
    // 1.动态给icon设置backgroundPosition
    var iconEls = document.querySelectorAll(".icon")
    for (var i = 0; i < iconEls.length; i++) {
      var iconEl = iconEls[i]
      iconEl.style.backgroundPosition = `-48px -${50*i}px`
    }
 
    // 2.实现鼠标进入动画
    // 方案一: mouseenter(不能使用事件委托)
    var itemEls = document.querySelectorAll(".item")
    for (var itemEl of itemEls) {
      itemEl.onmouseenter = function() {
        var nameEl = this.children[1]
        nameEl.style.width = "62px"
      }
      itemEl.onmouseleave = function() {
        var nameEl = this.children[1]
        nameEl.style.width = "0"
      }
    }
 
    // 方案二: mouseover(使用事件委托)
    // var toolbarEl = document.querySelector(".tool-bar")
    // toolbarEl.onmouseover = function(event) {
    //   handleMouseEvent(event, 62)
    // }
    // toolbarEl.onmouseout = function(event) {
    //   handleMouseEvent(event, 0)
    // }
 
    // function handleMouseEvent(event, width) {
    //   if (event.target !== toolbarEl) {
    //     // var itemEl = event.target.classList.contains("item") ? event.target: event.target.parentElement
    //     // 1.获取唯一的item
    //     var itemEl = null
    //     if (event.target.classList.contains("item")) {
    //       itemEl = event.target
    //     } else {
    //       itemEl = event.target.parentElement
    //     }
 
    //     // 2.根据item获取nameElement
    //     var nameEl = itemEl.children[1]
 
    //     // 3.设置宽度
    //     nameEl.style.width = `${width}px`
    //   }
    // }
 
  </script>
 
</body>
</html>
<!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>
  <style>
    .tool-bar {
      position: fixed;
      top: 30%;
      right: 0;
 
      /* display: flex;
      flex-direction: column;
      align-items: center; */
 
      width: 35px;
    }
 
    .item {
      position: relative;
      float: right;
      width: 35px;
      height: 35px;
      margin-bottom: 1px;
       
      background-color: #7a6e6e;
      border-radius: 3px 0 0 3px;
    }
 
    .icon {
      position: absolute;
      right: 0;
      top: 0;
      width: 35px;
      height: 35px;
      cursor: pointer;
      background-image: url(./img/toolbars.png);
       
      /* 禁止鼠标交互 */
      pointer-events: none;
    }
 
    /* .icon01 {
      background-position: -48px 0;
    }
    .icon02 {
      background-position: -48px -50px;
    }
    .icon03 {
      background-position: -48px -100px;
    }
    .icon04 {
      background-position: -48px -150px;
    } */
 
    .name {
      position: absolute;
      z-index: -1;
      right: 35px;
      top: 0;
       
      width: 0;
      height: 35px;
      line-height: 35px;
 
      color: #fff;
      text-align: center;
      font-size: 12px;
      background-color: #7a6e6e;
      cursor: pointer;
 
      border-radius: 3px 0 0 3px;
      transition: width .2s ease;
 
      /* 禁止鼠标交互 */
      pointer-events: none;
    }
 
    .item:hover,
    .item:hover .name {
      background-color: #cd1926;
    }
  </style>
</head>
<body>
   
  <div class="tool-bar">
    <div class="item">
      <i class="icon icon01"></i>
      <div class="name">购物车</div>
    </div>
    <div class="item">
      <i class="icon icon02"></i>
      <div class="name">收藏</div>
    </div>
    <div class="item">
      <i class="icon icon03"></i>
      <div class="name">限时活动</div>
    </div>
    <div class="item">
      <i class="icon icon04"></i>
      <div class="name">大礼包</div>
    </div>
  </div>
 
  <script>
    // 1.动态给icon设置backgroundPosition
    var iconEls = document.querySelectorAll(".icon")
    for (var i = 0; i < iconEls.length; i++) {
      var iconEl = iconEls[i]
      iconEl.style.backgroundPosition = `-48px -${50*i}px`
    }
 
    // 2.实现鼠标进入动画
    var toolbarEl = document.querySelector(".tool-bar")
    toolbarEl.onmouseover = function(event) {
      var nameEl = event.target.children[1]
      nameEl.style.width = "62px"
      event.target.style.width = `${62 + 35}px`
    }
    toolbarEl.onmouseout = function(event) {
      var nameEl = event.target.children[1]
      nameEl.style.width = "0"
      event.target.style.width = `${35}px`
    }
 
  </script>
 
</body>
</html>

案例实战5-王者荣耀tabControl

<!DOCTYPE html>
<html lang="zh-CN">
<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>王者荣耀-main-news</title>
  <link rel="stylesheet" href="./css/reset.css">
  <link rel="stylesheet" href="./css/common.css">
  <style>
    .main .section-content {
      display: flex;
      justify-content: space-between;
    }
 
    .main .section-content .left-content {
      width: 872px;
      height: 1000px;
    }
    .main .section-content .right-content {
      width: 295px;
      height: 500px;
    }
  </style>
</head>
<body>
   
  <div class="main main_wrapper">
    <div class="section-content">
      <div class="left-content">
        <div class="content-center">
          <div class="section_header">
            <div class="header_left">
              <h3 class="title">内容中心</h3>
            </div>
            <div class="header_right" href="#">
              <a class="more" href="#">更多</a>
            </div>
          </div>
          <div class="tab_control">
            <div class="item active">精品栏目</div>
            <div class="line"></div>
            <div class="item">赛事精品</div>
            <div class="line"></div>
            <div class="item">英雄攻略</div>
          </div>
        </div>
    </div>
  </div>
 
  <script>
    // 1.获取元素
    var tabControl = document.querySelector(".tab_control")
 
    // 2.监听鼠标进入(事件委托)
    var activeLiEl = tabControl.querySelector(".active")
    tabControl.onmouseover = function(event) {
      // 1.拿到事件发生的对象
      var itemEl = event.target
      if (itemEl.classList.contains("item")) {
        // 其他的取消active
        // 1.for循环所有的item
        // 2.querySelector(".active")
        // 3.记录当前的active对应的item
        activeLiEl.classList.remove("active")
 
        // 当前进入的item变成active
        itemEl.classList.add("active")
 
        // 将最新的itemEl变成activeLiEl
        activeLiEl = itemEl
      }
    }
 
  </script>
</body>
</html><!DOCTYPE html>
<html lang="zh-CN">
<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>王者荣耀-main-news</title>
  <link rel="stylesheet" href="./css/reset.css">
  <link rel="stylesheet" href="./css/common.css">
  <style>
    .main .section-content {
      display: flex;
      justify-content: space-between;
    }
 
    .main .section-content .left-content {
      width: 872px;
      height: 1000px;
    }
    .main .section-content .right-content {
      width: 295px;
      height: 500px;
    }
  </style>
</head>
<body>
   
  <div class="main main_wrapper">
    <div class="section-content">
      <div class="left-content">
        <div class="content-center">
          <div class="section_header">
            <div class="header_left">
              <h3 class="title">内容中心</h3>
            </div>
            <div class="header_right" href="#">
              <a class="more" href="#">更多</a>
            </div>
          </div>
          <div class="tab_control">
            <div class="item active">精品栏目</div>
            <div class="line"></div>
            <div class="item">赛事精品</div>
            <div class="line"></div>
            <div class="item">英雄攻略</div>
          </div>
        </div>
    </div>
  </div>
 
  <script>
    // 1.获取元素
    var tabControl = document.querySelector(".tab_control")
 
    // 2.监听鼠标进入(事件委托)
    var activeLiEl = tabControl.querySelector(".active")
    tabControl.onmouseover = function(event) {
      // 1.拿到事件发生的对象
      var itemEl = event.target
      if (itemEl.classList.contains("item")) {
        // 其他的取消active
        // 1.for循环所有的item
        // 2.querySelector(".active")
        // 3.记录当前的active对应的item
        activeLiEl.classList.remove("active")
 
        // 当前进入的item变成active
        itemEl.classList.add("active")
 
        // 将最新的itemEl变成activeLiEl
        activeLiEl = itemEl
      }
    }
 
  </script>
</body>
</html>

案例实战6-王者荣耀轮播图

基本实现

<!DOCTYPE html>
<html lang="zh-CN">
<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>王者荣耀-main-news</title>
  <link rel="stylesheet" href="./css/reset.css">
  <link rel="stylesheet" href="./css/common.css">
  <style>
    .main {
      height: 100px;
    }
 
    .news-section {
      display: flex;
      height: 342px;
    }
 
    .news-section .banner {
      width: 605px;
      background-color: #000;
      overflow: hidden;
    }
 
    .news-section .banner .image-list {
      display: flex;
      width: 604px;
      /* overflow: hidden; */
    }
 
    .news-section .banner .image-list .item {
      flex-shrink: 0;
      width: 100%;
    }
 
    .news-section .banner .image-list .item a {
      display: block;
    }
 
    .news-section .banner .image-list .item a img {
      width: 100%;
    }
 
    .news-section .banner .title-list {
      display: flex;
      height: 44px;
      line-height: 44px;
    }
 
    .news-section .banner .title-list .item {
      flex: 1;
      text-align: center;
    }
 
    .news-section .banner .title-list .item a {
      display: block;
      font-size: 14px;
      color: #b1b2be;
    }
    .news-section .banner .title-list .item.active a,
    .news-section .banner .title-list .item a:hover {
      color: #f3c258;
      background-color: rgba(255,255,255,.15);
    }
 
    .news-section .news {
      flex: 1;
      background-color: purple;
    }
 
    .news-section .download {
      width: 236px;
      background-color: skyblue;
    }
 
    .news-section .download a {
      display: block;
      background: url(./img/main_sprite.png) no-repeat;
    }
 
    .news-section .download a.download-btn {
      height: 128px;
      background-position: 0 -219px;
    }
 
    .news-section .download a.guard-btn {
      height: 106px;
      background-position: 0 -350px;
    }
 
    .news-section .download a.experience-btn {
      height: 108px;
      background-position: 0 -461px;
    }
  </style>
</head>
<body>
   
  <div class="main main_wrapper">
    <div class="news-section">
      <div class="banner">
        <ul class="image-list">
          <li class="item">
            <a href="">
              <img src="./img/banner_01.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_02.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_03.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_04.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_05.jpeg" alt="">
            </a>
          </li>
        </ul>
        <ul class="title-list">
          <li class="item active">
            <a href="#">桑启的旅途故事</a>
          </li>
          <li class="item">
            <a href="#">启示之音抢先听</a>
          </li>
          <li class="item">
            <a href="#">谁成为版本之子</a>
          </li>
          <li class="item">
            <a href="#">观赛体验升级</a>
          </li>
          <li class="item">
            <a href="#">季后赛开战</a>
          </li>
        </ul>
      </div>
      <div class="news"></div>
      <div class="download">
        <a class="download-btn" href="#"></a>
        <a class="guard-btn" href="#"></a>
        <a class="experience-btn" href="#"></a>
      </div>
    </div>
  </div>
 
  <script>
    // 1.获取元素
    var titleListEl = document.querySelector(".title-list")
    var activeItemEl = document.querySelector(".active")
     
    var imageListEl = document.querySelector(".image-list")
 
    // 2.底部titles的切换, 同时进行轮播
    titleListEl.onmouseover = function(event) {
      // 1.1.确定发生鼠标进入的元素
      var itemEl = event.target.parentElement
      if (!itemEl.classList.contains("item")) return
 
      // 1.2.移除之前的active
      activeItemEl.classList.remove("active")
 
      // 1.3.将active添加到鼠标进入的元素
      itemEl.classList.add("active")
 
      // 1.4.让activeItemEl指向最新的元素
      activeItemEl = itemEl
 
      // 1.5.移动对应的imageListEl
      // 1.5.1. 获取itemEl所在的索引
      // for (var i = 0; i < titleListEl.children.length; i++) {
      //   if (titleListEl.children[i] === itemEl) break
      // }
      var index = Array.from(titleListEl.children).findIndex(function(item) {
        return item === itemEl
      })
 
      imageListEl.style.transform = `translateX(${-604 * index}px)`
      imageListEl.style.transition = `all 300ms ease`
    }
 
  </script>
 
</body>
</html>

添加定时器 

<!DOCTYPE html>
<html lang="zh-CN">
<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>王者荣耀-main-news</title>
  <link rel="stylesheet" href="./css/reset.css">
  <link rel="stylesheet" href="./css/common.css">
  <style>
    .main {
      height: 100px;
    }
 
    .news-section {
      display: flex;
      height: 342px;
    }
 
    .news-section .banner {
      width: 605px;
      background-color: #000;
      overflow: hidden;
    }
 
    .news-section .banner .image-list {
      display: flex;
      width: 604px;
      /* overflow: hidden; */
    }
 
    .news-section .banner .image-list .item {
      flex-shrink: 0;
      width: 100%;
    }
 
    .news-section .banner .image-list .item a {
      display: block;
    }
 
    .news-section .banner .image-list .item a img {
      width: 100%;
    }
 
    .news-section .banner .title-list {
      display: flex;
      height: 44px;
      line-height: 44px;
    }
 
    .news-section .banner .title-list .item {
      flex: 1;
      text-align: center;
    }
 
    .news-section .banner .title-list .item a {
      display: block;
      font-size: 14px;
      color: #b1b2be;
    }
    .news-section .banner .title-list .item.active a,
    .news-section .banner .title-list .item a:hover {
      color: #f3c258;
      background-color: rgba(255,255,255,.15);
    }
 
    .news-section .news {
      flex: 1;
      background-color: purple;
    }
 
    .news-section .download {
      width: 236px;
      background-color: skyblue;
    }
 
    .news-section .download a {
      display: block;
      background: url(./img/main_sprite.png) no-repeat;
    }
 
    .news-section .download a.download-btn {
      height: 128px;
      background-position: 0 -219px;
    }
 
    .news-section .download a.guard-btn {
      height: 106px;
      background-position: 0 -350px;
    }
 
    .news-section .download a.experience-btn {
      height: 108px;
      background-position: 0 -461px;
    }
  </style>
</head>
<body>
   
  <div class="main main_wrapper">
    <div class="news-section">
      <div class="banner">
        <ul class="image-list">
          <li class="item">
            <a href="">
              <img src="./img/banner_01.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_02.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_03.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_04.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_05.jpeg" alt="">
            </a>
          </li>
        </ul>
        <ul class="title-list">
          <li class="item active">
            <a href="#">桑启的旅途故事</a>
          </li>
          <li class="item">
            <a href="#">启示之音抢先听</a>
          </li>
          <li class="item">
            <a href="#">谁成为版本之子</a>
          </li>
          <li class="item">
            <a href="#">观赛体验升级</a>
          </li>
          <li class="item">
            <a href="#">季后赛开战</a>
          </li>
        </ul>
      </div>
      <div class="news"></div>
      <div class="download">
        <a class="download-btn" href="#"></a>
        <a class="guard-btn" href="#"></a>
        <a class="experience-btn" href="#"></a>
      </div>
    </div>
  </div>
 
  <script>
    // 1.获取元素
    var titleListEl = document.querySelector(".title-list")
    var imageListEl = document.querySelector(".image-list")
 
    // 定义变量保存一些的状态
    var activeItemEl = document.querySelector(".active")
    var currentIndex = 0
 
    // 2.底部titles的切换, 同时进行轮播
    titleListEl.onmouseover = function(event) {
      // 1.1.确定发生鼠标进入的元素
      var itemEl = event.target.parentElement
      if (!itemEl.classList.contains("item")) return
 
      // 1.2.移除之前的active
      activeItemEl.classList.remove("active")
 
      // 1.3.将active添加到鼠标进入的元素
      itemEl.classList.add("active")
 
      // 1.4.让activeItemEl指向最新的元素
      activeItemEl = itemEl
 
      // 1.5.移动对应的imageListEl
      var index = Array.from(titleListEl.children).findIndex(function(item) {
        return item === itemEl
      })
 
      imageListEl.style.transform = `translateX(${-604 * index}px)`
      imageListEl.style.transition = `all 300ms ease`
 
      currentIndex = index
    }
 
    // 3.定时器: 定时轮播
    setInterval(function() {
      currentIndex++
      if (currentIndex === titleListEl.children.length) {
        currentIndex = 0
      }
 
      imageListEl.style.transform = `translateX(${-604 * currentIndex}px)`
      imageListEl.style.transition = `all 300ms ease`
 
      // 处理titles中的li的active
      // 1> 移除之前的active
      activeItemEl.classList.remove("active")
 
      // 2> 添加新的active
      var currentItemEl = titleListEl.children[currentIndex]
      currentItemEl.classList.add("active")
 
      // 3> 记录新的activeLi
      activeItemEl = currentItemEl
    }, 3000);
 
  </script>
 
</body>
</html>

代码的重构

<!DOCTYPE html>
<html lang="zh-CN">
<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>王者荣耀-main-news</title>
  <link rel="stylesheet" href="./css/reset.css">
  <link rel="stylesheet" href="./css/common.css">
  <style>
    .main {
      height: 100px;
    }
 
    .news-section {
      display: flex;
      height: 342px;
    }
 
    .news-section .banner {
      width: 605px;
      background-color: #000;
      overflow: hidden;
    }
 
    .news-section .banner .image-list {
      display: flex;
      width: 604px;
      /* overflow: hidden; */
    }
 
    .news-section .banner .image-list .item {
      flex-shrink: 0;
      width: 100%;
    }
 
    .news-section .banner .image-list .item a {
      display: block;
    }
 
    .news-section .banner .image-list .item a img {
      width: 100%;
    }
 
    .news-section .banner .title-list {
      display: flex;
      height: 44px;
      line-height: 44px;
    }
 
    .news-section .banner .title-list .item {
      flex: 1;
      text-align: center;
    }
 
    .news-section .banner .title-list .item a {
      display: block;
      font-size: 14px;
      color: #b1b2be;
    }
    .news-section .banner .title-list .item.active a,
    .news-section .banner .title-list .item a:hover {
      color: #f3c258;
      background-color: rgba(255,255,255,.15);
    }
 
    .news-section .news {
      flex: 1;
      background-color: purple;
    }
 
    .news-section .download {
      width: 236px;
      background-color: skyblue;
    }
 
    .news-section .download a {
      display: block;
      background: url(./img/main_sprite.png) no-repeat;
    }
 
    .news-section .download a.download-btn {
      height: 128px;
      background-position: 0 -219px;
    }
 
    .news-section .download a.guard-btn {
      height: 106px;
      background-position: 0 -350px;
    }
 
    .news-section .download a.experience-btn {
      height: 108px;
      background-position: 0 -461px;
    }
  </style>
</head>
<body>
   
  <div class="main main_wrapper">
    <div class="news-section">
      <div class="banner">
        <ul class="image-list">
          <li class="item">
            <a href="">
              <img src="./img/banner_01.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_02.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_03.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_04.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_05.jpeg" alt="">
            </a>
          </li>
        </ul>
        <ul class="title-list">
          <li class="item active">
            <a href="#">桑启的旅途故事</a>
          </li>
          <li class="item">
            <a href="#">启示之音抢先听</a>
          </li>
          <li class="item">
            <a href="#">谁成为版本之子</a>
          </li>
          <li class="item">
            <a href="#">观赛体验升级</a>
          </li>
          <li class="item">
            <a href="#">季后赛开战</a>
          </li>
        </ul>
      </div>
      <div class="news"></div>
      <div class="download">
        <a class="download-btn" href="#"></a>
        <a class="guard-btn" href="#"></a>
        <a class="experience-btn" href="#"></a>
      </div>
    </div>
  </div>
 
  <script>
    // 1.获取元素
    var titleListEl = document.querySelector(".title-list")
    var imageListEl = document.querySelector(".image-list")
 
    // 定义变量保存一些的状态
    var activeItemEl = document.querySelector(".active")
    var currentIndex = 0
 
    // 2.底部titles的切换, 同时进行轮播
    titleListEl.onmouseover = function(event) {
      // 1.1.确定发生鼠标进入的元素
      var itemEl = event.target.parentElement
      if (!itemEl.classList.contains("item")) return
 
      // 1.2.获取对应的索引index
      var index = Array.from(titleListEl.children).findIndex(function(item) {
        return item === itemEl
      })
      currentIndex = index
 
      // 1.3.调用切换的函数
      switchBanner()
    }
 
    // 3.定时器: 定时轮播
    setInterval(function() {
      currentIndex++
      if (currentIndex === titleListEl.children.length) {
        currentIndex = 0
      }
 
      // 调用切换的函数
      switchBanner()
    }, 3000);
 
 
    // 封装一个切换轮播的函数
    function switchBanner() {
      // 第一件事情: 让imageListEl滚动
      // 1.1.让imageListEl修改transform
      imageListEl.style.transform = `translateX(${-604 * currentIndex}px)`
      imageListEl.style.transition = `all 300ms ease`
 
      // 第二件事情: 改变title选中
      // 1.2.移除之前的active
      activeItemEl.classList.remove("active")
 
      // 1.3.将active添加到鼠标进入的元素
      var currentItemEl = titleListEl.children[currentIndex]
      currentItemEl.classList.add("active")
 
      // 1.4.让activeItemEl指向最新的元素
      activeItemEl = currentItemEl
    }
  </script>
 
</body>
</html>

移除定时器

<!DOCTYPE html>
<html lang="zh-CN">
<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>王者荣耀-main-news</title>
  <link rel="stylesheet" href="./css/reset.css">
  <link rel="stylesheet" href="./css/common.css">
  <style>
    .main {
      height: 100px;
    }
 
    .news-section {
      display: flex;
      height: 342px;
    }
 
    .news-section .banner {
      width: 605px;
      background-color: #000;
      overflow: hidden;
    }
 
    .news-section .banner .image-list {
      display: flex;
      width: 604px;
      /* overflow: hidden; */
    }
 
    .news-section .banner .image-list .item {
      flex-shrink: 0;
      width: 100%;
    }
 
    .news-section .banner .image-list .item a {
      display: block;
    }
 
    .news-section .banner .image-list .item a img {
      width: 100%;
    }
 
    .news-section .banner .title-list {
      display: flex;
      height: 44px;
      line-height: 44px;
    }
 
    .news-section .banner .title-list .item {
      flex: 1;
      text-align: center;
    }
 
    .news-section .banner .title-list .item a {
      display: block;
      font-size: 14px;
      color: #b1b2be;
    }
    .news-section .banner .title-list .item.active a,
    .news-section .banner .title-list .item a:hover {
      color: #f3c258;
      background-color: rgba(255,255,255,.15);
    }
 
    .news-section .news {
      flex: 1;
      background-color: purple;
    }
 
    .news-section .download {
      width: 236px;
      background-color: skyblue;
    }
 
    .news-section .download a {
      display: block;
      background: url(./img/main_sprite.png) no-repeat;
    }
 
    .news-section .download a.download-btn {
      height: 128px;
      background-position: 0 -219px;
    }
 
    .news-section .download a.guard-btn {
      height: 106px;
      background-position: 0 -350px;
    }
 
    .news-section .download a.experience-btn {
      height: 108px;
      background-position: 0 -461px;
    }
  </style>
</head>
<body>
   
  <div class="main main_wrapper">
    <div class="news-section">
      <div class="banner">
        <ul class="image-list">
          <li class="item">
            <a href="">
              <img src="./img/banner_01.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_02.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_03.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_04.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_05.jpeg" alt="">
            </a>
          </li>
        </ul>
        <ul class="title-list">
          <li class="item active">
            <a href="#">桑启的旅途故事</a>
          </li>
          <li class="item">
            <a href="#">启示之音抢先听</a>
          </li>
          <li class="item">
            <a href="#">谁成为版本之子</a>
          </li>
          <li class="item">
            <a href="#">观赛体验升级</a>
          </li>
          <li class="item">
            <a href="#">季后赛开战</a>
          </li>
        </ul>
      </div>
      <div class="news"></div>
      <div class="download">
        <a class="download-btn" href="#"></a>
        <a class="guard-btn" href="#"></a>
        <a class="experience-btn" href="#"></a>
      </div>
    </div>
  </div>
 
  <script>
    // 1.获取元素
    var titleListEl = document.querySelector(".title-list")
    var imageListEl = document.querySelector(".image-list")
    var bannerEl = document.querySelector(".banner")
 
    // 定义变量保存一些的状态
    var activeItemEl = document.querySelector(".active")
    var currentIndex = 0
    var timerID = null
 
    // 2.底部titles的切换, 同时进行轮播
    titleListEl.onmouseover = function(event) {
      // 1.1.确定发生鼠标进入的元素
      var itemEl = event.target.parentElement
      if (!itemEl.classList.contains("item")) return
 
      // 1.2.获取对应的索引index
      var index = Array.from(titleListEl.children).findIndex(function(item) {
        return item === itemEl
      })
      currentIndex = index
 
      // 1.3.调用切换的函数
      switchBanner()
    }
 
    // 3.定时器: 定时轮播
    startTimer()
 
 
    // 监听banner的事件
    bannerEl.onmouseenter = function() {
      clearInterval(timerID)
    }
    bannerEl.onmouseleave = function() {
      startTimer()
    }
 
 
    // 封装一个切换轮播的函数
    function switchBanner() {
      // 第一件事情: 让imageListEl滚动
      // 1.1.让imageListEl修改transform
      imageListEl.style.transform = `translateX(${-604 * currentIndex}px)`
      imageListEl.style.transition = `all 300ms ease`
 
      // 第二件事情: 改变title选中
      // 1.2.移除之前的active
      activeItemEl.classList.remove("active")
 
      // 1.3.将active添加到鼠标进入的元素
      var currentItemEl = titleListEl.children[currentIndex]
      currentItemEl.classList.add("active")
 
      // 1.4.让activeItemEl指向最新的元素
      activeItemEl = currentItemEl
    }
 
    // 封装一个添加定时器的函数
    function startTimer() {
      timerID = setInterval(function() {
        currentIndex++
        if (currentIndex === titleListEl.children.length) {
          currentIndex = 0
        }
 
        // 调用切换的函数
        switchBanner()
      }, 3000);
    }
 
  </script>
 
</body>
</html>

默认的效果

<!DOCTYPE html>
<html lang="zh-CN">
<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>王者荣耀-main-news</title>
  <link rel="stylesheet" href="./css/reset.css">
  <link rel="stylesheet" href="./css/common.css">
  <style>
    .main {
      height: 100px;
    }
 
    .news-section {
      display: flex;
      height: 342px;
    }
 
    .news-section .banner {
      width: 605px;
      background-color: #000;
      overflow: hidden;
    }
 
    .news-section .banner .image-list {
      position: relative;
      display: flex;
      width: 604px;
      height: 298px;
    }
 
    .news-section .banner .image-list .item {
      position: absolute;
      left: 100%;
      flex-shrink: 0;
      width: 100%;
    }
 
    .news-section .banner .image-list .item:first-child {
      left: 0;
      transition: left 300ms ease;
    }
 
    .news-section .banner .image-list .item a {
      display: block;
    }
 
    .news-section .banner .image-list .item a img {
      width: 100%;
    }
 
    .news-section .banner .title-list {
      display: flex;
      height: 44px;
      line-height: 44px;
    }
 
    .news-section .banner .title-list .item {
      flex: 1;
      text-align: center;
    }
 
    .news-section .banner .title-list .item a {
      display: block;
      font-size: 14px;
      color: #b1b2be;
    }
    .news-section .banner .title-list .item.active a,
    .news-section .banner .title-list .item a:hover {
      color: #f3c258;
      background-color: rgba(255,255,255,.15);
    }
 
    .news-section .news {
      flex: 1;
      background-color: purple;
    }
 
    .news-section .download {
      width: 236px;
      background-color: skyblue;
    }
 
    .news-section .download a {
      display: block;
      background: url(./img/main_sprite.png) no-repeat;
    }
 
    .news-section .download a.download-btn {
      height: 128px;
      background-position: 0 -219px;
    }
 
    .news-section .download a.guard-btn {
      height: 106px;
      background-position: 0 -350px;
    }
 
    .news-section .download a.experience-btn {
      height: 108px;
      background-position: 0 -461px;
    }
  </style>
</head>
<body>
   
  <div class="main main_wrapper">
    <div class="news-section">
      <div class="banner">
        <ul class="image-list">
          <li class="item">
            <a href="">
              <img src="./img/banner_01.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_02.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_03.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_04.jpeg" alt="">
            </a>
          </li>
          <li class="item">
            <a href="">
              <img src="./img/banner_05.jpeg" alt="">
            </a>
          </li>
        </ul>
        <ul class="title-list">
          <li class="item active">
            <a href="#">桑启的旅途故事</a>
          </li>
          <li class="item">
            <a href="#">启示之音抢先听</a>
          </li>
          <li class="item">
            <a href="#">谁成为版本之子</a>
          </li>
          <li class="item">
            <a href="#">观赛体验升级</a>
          </li>
          <li class="item">
            <a href="#">季后赛开战</a>
          </li>
        </ul>
      </div>
      <div class="news"></div>
      <div class="download">
        <a class="download-btn" href="#"></a>
        <a class="guard-btn" href="#"></a>
        <a class="experience-btn" href="#"></a>
      </div>
    </div>
  </div>
 
  <script>
    // 1.获取元素
    var titleListEl = document.querySelector(".title-list")
    var imageListEl = document.querySelector(".image-list")
    var bannerEl = document.querySelector(".banner")
 
    // 定义变量保存一些的状态
    var activeTitleEl = titleListEl.querySelector(".active")
    var currentIndex = 0
    var previousIndex = 0
    var timerID = null
 
    // 2.底部titles的切换, 同时进行轮播
    titleListEl.onmouseover = function(event) {
      // 1.1.确定发生鼠标进入的元素
      var itemEl = event.target.parentElement
      if (!itemEl.classList.contains("item")) return
 
      // 1.2.获取对应的索引index
      var index = Array.from(titleListEl.children).findIndex(function(item) {
        return item === itemEl
      })
      previousIndex = currentIndex
      currentIndex = index
 
      // 1.3.调用切换的函数
      switchBanner()
    }
 
    // 3.定时器: 定时轮播
    startTimer()
 
 
    // 监听banner的事件
    bannerEl.onmouseenter = function() {
      clearInterval(timerID)
    }
    bannerEl.onmouseleave = function() {
      startTimer()
    }
 
 
    // 封装一个切换轮播的函数
    function switchBanner() {
      // 第一件事情: 让imageListEl滚动
      // 1.1.让imageListEl修改transform
      // 其他内容需要调整
      for (var i = 0; i < imageListEl.children.length; i++) {
        var itemEl = imageListEl.children[i]
 
        if (i === currentIndex) { // 当前要展示的imageItem
          itemEl.style.transition = "left 300ms ease"
          itemEl.style.left = "0"
        } else if (i < currentIndex) { // 需要放到左侧的imageItem
          if (i !== previousIndex) {
            itemEl.style.transition = "none"
          }
          itemEl.style.left = "-100%"
        } else { // 需要放到右侧的imageItem
          if (i !== previousIndex) {
            itemEl.style.transition = "none"
          }
          itemEl.style.left = "100%"
        }
      }
 
 
      // 第二件事情: 改变title选中
      // 1.2.移除之前的active
      activeTitleEl.classList.remove("active")
 
      // 1.3.将active添加到鼠标进入的元素
      var currentItemEl = titleListEl.children[currentIndex]
      currentItemEl.classList.add("active")
 
      // 1.4.让activeItemEl指向最新的元素
      activeTitleEl = currentItemEl
    }
 
    // 封装一个添加定时器的函数
    function startTimer() {
      timerID = setInterval(function() {
        previousIndex = currentIndex
        currentIndex++
        if (currentIndex === titleListEl.children.length) {
          currentIndex = 0
        }
 
        // 调用切换的函数
        switchBanner()
      }, 3000);
    }
 
  </script>
 
</body>
</html>

案例实战7-书籍购物车

基本搭建

<!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>
  <style>
    table {
      border-collapse: collapse;
    }
 
    thead {
      background-color: #f5f5f5;
    }
 
    th, td {
      border: 1px solid #aaa;
      padding: 8px 12px;
      text-align: center;
    }
  </style>
</head>
<body>
 
  <table>
    <thead>
      <tr>
        <th>编号</th>
        <th>书籍名称</th>
        <th>出版日期</th>
        <th>价格</th>
        <th>购买数量</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
    </tbody>
  </table>
  <h2 class="price">
    总价格: ¥<span class="price-count">0</span>
  </h2>
   
  <script>
    // 1.从服务器获取数据 ajax/fetch
    var books = [
      {
        id: 1,
        name: '《算法导论》',
        date: '2006-09',
        price: 85.00,
        count: 3
      },
      {
        id: 2,
        name: '《UNIX编程艺术》',
        date: '2006-02',
        price: 59.00,
        count: 2
      },
      {
        id: 3,
        name: '《编程珠玑》',
        date: '2008-10',
        price: 39.00,
        count: 5
      },
      {
        id: 4,
        name: '《代码大全》',
        date: '2006-03',
        price: 128.00,
        count: 8
      }
    ]
 
 
    // 2.对数据展示
    // 到底通过html直接编写, 还是通过JavaScriptDOM操作创建元素
    // 1> 对于固定的, 直接通过html编写(能通过html编写, 尽量通过html直接编写)
    // 2> 对于哪些大量的数据, 有规律的数据, 可以通过JavaScript编写
    var tbodyEl = document.querySelector("tbody")
     
    // 2.2. 动态添加tr以及内部数据
    for (var i = 0; i < books.length; i++) {
      var trowEl = document.createElement("tr")
 
      // 2.3. 放具体数据
      var book = books[i]
      var bookKeys = Object.keys(book)
      for (var m = 0; m < bookKeys.length; m++) {
        var key = bookKeys[m]
        var value = book[key]
        var tdEl = document.createElement("td")
        if (key === "price") {
          value = "¥" + value
        }
        tdEl.textContent = value
        trowEl.append(tdEl)
      }
 
      // 2.4. 添加删除按钮
      var deleteTdEl = document.createElement("td")
      var deleteBtnEl = document.createElement("button")
      deleteBtnEl.textContent = "删除"
      deleteTdEl.append(deleteBtnEl)
      trowEl.append(deleteTdEl)
 
 
      tbodyEl.append(trowEl)
    }
 
     
    // 3.计算总价格
    var priceCountEl = document.querySelector(".price-count")
    // var totalPrice = 0
    // for (var i = 0; i < books.length; i++) {
    //   totalPrice += books[i].count * books[i].price
    // }
    var totalPrice = books.reduce(function(preValue, item) {
      return preValue + item.count * item.price
    }, 0)
    priceCountEl.textContent = totalPrice
 
  </script>
 
</body>
</html>

删除操作

<!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>
  <style>
    table {
      border-collapse: collapse;
    }
 
    thead {
      background-color: #f5f5f5;
    }
 
    th, td {
      border: 1px solid #aaa;
      padding: 8px 12px;
      text-align: center;
    }
  </style>
</head>
<body>
 
  <table>
    <thead>
      <tr>
        <th>编号</th>
        <th>书籍名称</th>
        <th>出版日期</th>
        <th>价格</th>
        <th>购买数量</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
    </tbody>
  </table>
  <h2 class="price">
    总价格: ¥<span class="price-count">0</span>
  </h2>
   
  <script>
    // 1.从服务器获取数据 ajax/fetch
    var books = [
      {
        id: 1,
        name: '《算法导论》',
        date: '2006-09',
        price: 85.00,
        count: 3
      },
      {
        id: 2,
        name: '《UNIX编程艺术》',
        date: '2006-02',
        price: 59.00,
        count: 2
      },
      {
        id: 3,
        name: '《编程珠玑》',
        date: '2008-10',
        price: 39.00,
        count: 5
      },
      {
        id: 4,
        name: '《代码大全》',
        date: '2006-03',
        price: 128.00,
        count: 8
      }
    ]
 
 
    // 2.对数据展示
    // 到底通过html直接编写, 还是通过JavaScriptDOM操作创建元素
    // 1> 对于固定的, 直接通过html编写(能通过html编写, 尽量通过html直接编写)
    // 2> 对于哪些大量的数据, 有规律的数据, 可以通过JavaScript编写
    var tbodyEl = document.querySelector("tbody")
     
    // 2.2. 动态添加tr以及内部数据
    for (var i = 0; i < books.length; i++) {
      var trowEl = document.createElement("tr")
 
      // 2.3. 放具体数据
      var book = books[i]
      var bookKeys = Object.keys(book)
      for (var m = 0; m < bookKeys.length; m++) {
        var key = bookKeys[m]
        var value = book[key]
        var tdEl = document.createElement("td")
        if (key === "price") {
          value = "¥" + value
        }
        tdEl.textContent = value
        trowEl.append(tdEl)
      }
 
      // 2.4. 添加删除按钮
      var deleteTdEl = document.createElement("td")
      var deleteBtnEl = document.createElement("button")
      deleteBtnEl.textContent = "删除"
      deleteTdEl.append(deleteBtnEl)
      trowEl.append(deleteTdEl)
 
      // 2.5.监听删除按钮的点击
      deleteBtnEl.onclick = function() {
        // 1.删除对应的trow
        var deleteTRowEl = this.parentElement.parentElement
        var deleteTrIndex = deleteTRowEl.sectionRowIndex
        deleteTRowEl.remove()
 
        // 2.删除对应books中的数据
        books.splice(deleteTrIndex, 1)
 
        // 3.重新计算一次价格
        calcTotalPrice()
      }
 
      tbodyEl.append(trowEl)
    }
 
     
    // 3.计算总价格
    var priceCountEl = document.querySelector(".price-count")
    calcTotalPrice()
 
    // 封装计算价格的函数
    function calcTotalPrice() {
      var totalPrice = books.reduce(function(preValue, item) {
        return preValue + item.count * item.price
      }, 0)
      priceCountEl.textContent = totalPrice
    }
 
  </script>
 
</body>
</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/601817.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

使用 Kotlin 实现 SOLID 原则编写清晰易维护的代码

使用 Kotlin 实现 SOLID 原则编写清晰易维护的代码 在编写软件时&#xff0c;很容易陷入特定编程语言&#xff0c;库和工具的细节中。然而&#xff0c;良好的软件设计不是与任何特定技术相关联的&#xff1b;相反&#xff0c;它基于一组经过多年和多个项目证明有效的原则。其中…

量子计算:纠错码 量子算法

文章目录 量子纠错码Shor 码CSS 码Steane 码一般性错误容错量子计算 量子算法量子 Fourier 变换Shor 算法求阶问题&#xff08;整数分解&#xff09;求周期问题&#xff08;离散对数&#xff09; Grover 算法 量子纠错码 对于量子态的纠错&#xff0c;与经典信息论中的纠错有着…

ATTCK v13版本战术介绍——防御规避(七)

一、引言 在前几期文章中我们介绍了ATT&CK中侦察、资源开发、初始访问、执行、持久化、提权战术理论知识及实战研究、部分防御规避战术&#xff0c;本期我们为大家介绍ATT&CK 14项战术中防御规避战术第37-42种子技术&#xff0c;后续会介绍其他技术&#xff0c;敬请关…

2000-2020全要素生产率OP法+LP法+OLS和固定效应法三种方法合集含原始数据和计算过程Stata代码

2000-2020全要素生产率OP法LP法OLS和固定效应法三种方法合集含原始数据和计算过程Stata代码 1、时间&#xff1a;OP法&#xff1a;2008-2020年、LP法2000-2020年、OLS和固定效应法2000-2020年 2、数据内容&#xff1a;包括原始数据、计算结果和stata do文档 3、方法说明&…

node版本管理(Windows)

node版本管理&#xff08;Windows&#xff09;&#xff0c;使用 nvm 进行node版本管理 1、如果电脑安装有node&#xff0c;需要先卸载 2、安装 nvm 管理工具&#xff0c;nvm 官网地址&#xff1a;https://github.com/coreybutler/nvm-windows/releases 3、将下载下来的压缩包…

双向链表详解

目录 一&#xff0c;双向链表的概念及结构 二&#xff0c;双向链表的方法及其实现 2.1 双向链表 2.2 addFirst(int data) - 头插法 2.3 addLast(int data) - 尾插法 2.4 size() - 链表长度 2.5 display() - 打印链表内容 2.6 clear() - 删除链表 2.7 addIndex(int in…

TOOM舆情监控与舆情传播:塑造有益信息环境

随着互联网和社交媒体的快速发展&#xff0c;舆情传播成为了影响社会舆论和公众意见的重要因素。然而&#xff0c;不可避免地&#xff0c;虚假信息、谣言和负面舆情也随之而来&#xff0c;对公众和社会造成了负面影响。在这样的背景下&#xff0c;舆情监控作为一种强有力的工具…

一出社会就在外包划水5年,已经废了

要不是女朋友和我提分手&#xff0c;我估计现在还没醒悟 本科大专&#xff0c;17年通过校招进入某软件公司做测试&#xff0c;干了接近5年的功能。 今年年初&#xff0c;感觉自己不能够在这样下去了&#xff0c;长时间呆在一个舒适的环境会让一个人堕落&#xff01;而我已经在…

uboot的使用

目录 串口调试 1.uboot模式 自启动模式&#xff1a; 交互模式 2.uboot帮助命令 3.uboot环境变量 4.uboot常用环境变量 5.uboot网络传输命令 6.uboot存储器访问命令 7.uboot自启动环境变量 串口调试 1.串口连接开发板&#xff0c;通过 "设备管理器" 获取对…

JavaWeb概述

WEB服务器软件: 从Browser端发送数据到server端&#xff0c;我们称为请求&#xff0c;request 从Server端向浏览器端发送数据&#xff0c;我们称为相应&#xff0c;英语单词&#xff0c;response 关于Tomcat服务器的目录: bin:这个目录是Tomcat服务器的命令文件存放的目录&…

[SpringBoot]MyBatis Plus框架使用selectCount

目录 MyBatis Plus介绍 CRUD操作使用 添加依赖: 接口应该继承自BaseMapper 在继承自BaseMapper的泛型实体类上使用TableName注解指定数据表的名称 在类中与主键对应的属性上使用TableId注解指定主键的值如何处理 另外&#xff0c;原本应该做的配置不变&#xff0c;例如在…

OPC UA 云端模型库

UA 云库&#xff08;opc ua cloud library&#xff09;是互联网上可用的中央库&#xff0c;可以在其中上传配套规范 &#xff08;CS&#xff09; 并将其提供给其他人。许多CS是由OPC基金会的联合工作组开发和发布的。来自不同行业和应用领域的几位专家共同努力&#xff0c;为许…

水库大坝安全监测具体内容

水库大坝实时监测的主要任务是实时监测各个监测点水库水位、水压、渗流、流量、扬压力等&#xff0c;用无线传感网络完成数据传输&#xff0c;在计算机上用数据模式或图形模式反映出来&#xff0c;实时掌控整个水库大坝各项变化情况&#xff0c;特殊数据实行声光报警。大坝安全…

面试官:深拷贝与浅拷贝有啥区别?

文章目录 1.前言2.基本类型的拷贝3.引用类型的拷贝3.1 关于引用类型的浅拷贝3.2 关于引用类型的深拷贝 1.前言 首先&#xff0c;明确一点深拷贝和浅拷贝是针对对象属性为对象的&#xff0c;因为基本数据类型在进行赋值操作时&#xff08;也就是拷贝&#xff09;是直接将值赋给…

官宣!Databend 和 XSKY星辰天合达成合作

近日&#xff0c;北京数变科技有限公司与北京星辰天合科技股份有限公司完成了产品兼容性适配互认证。 本次测试是对 Databend 云原生数据仓库平台与星辰天合企业级存储产品&超融合产品进行严格的联合测试验证&#xff0c;结果显示&#xff0c;双方产品完全兼容&#xff0c;…

日撸java三百行day58

文章目录 说明Day58 符号型数据的 NB 算法1.基础理论知识1.1 条件概率1.2 独立性假设1.3 Laplacian 平滑 2. 符号型数据的预测算法跟踪2.1 testNominal()方法2.1.1 NaiveBayes 构造函数2.1.2 calculateClassDistribution()2.1.3 calculateConditionalProbabilities()方法2.1.4 …

STM32F4_SPI协议详解

目录 1. 什么是SPI 2. SPI物理层 3. SPI协议层 3.1 SPI基本通讯过程 3.2 数据有效性 3.3 CPOL/CPHA及通讯模式 4. SPI框图及通讯过程 4.1 SPI框图 4.2 通讯过程 5. SPI初始化结构体 6. Flash芯片(W25Q128)简介 7. 库函数配置SPI1的主模式 8. 实验程序 8.1 实验程…

“金九银十”是找工作的最佳时期吗?那倒未必

金九银十找工作 优势&#xff1a; 供选择的公司多&#xff0c;机会多 劣势&#xff1a; 人才供应量旺盛 成为备胎的几率大增&#xff0c;获取offer的时间较慢 若无明显竞争力&#xff0c;薪资涨幅相对不会太高 比起那些在跳槽季(金三银四&#xff0c;金九银十)扎堆找工作…

【LED子系统深度剖析】九、数据结构详解(番外篇)

个人主页:董哥聊技术 我是董哥,高级嵌入式软件开发工程师,从事嵌入式Linux驱动开发和系统开发,曾就职于世界500强公司! 创作理念:专注分享高质量嵌入式文章,让大家读有所得! 文章目录 1、核心数据结构1.1 gpio_led_platform_data1.2 gpio_leds_priv1.3 gpio_led1.4 gpi…

2022年营收31.88亿,国产模拟 IC 头部企业持续扩充品类促发展

国产IC增速快于全球 IC &#xff0c; 国产替代空间广阔 根据 WSTS 的数据&#xff0c;2021 年全球 IC 市场规模高增 28.2%&#xff0c;2022 年全球 IC 市场规模同比增速放缓至 3.7%&#xff0c;由于需求减弱&#xff0c;且全球各下游仍在消化库存&#xff0c;预计 2023 年全球…