js学习第十二天

节点操作 (增  删  改  查)

通过节点关系查找元素

1  元素.parentNode

 document.querySelector('.close').addEventListener('click', function () {
        this.parentNode.style.display = 'none'
      })

2 获取子节点 元素.children

 const ul = document.querySelector('ul')
      console.log(Array.isArray(ul.children))
      console.log(ul.children) // 伪数组 本质是对象 {0:...,1:***, length:20}
      console.log(ul.childNodes) // 了解   所有儿子 包括文本节点
      for (let i = 0; i < ul.children.length; i++) {
        console.log(ul.children[i])
      }

3 兄弟节点

 const country = document.querySelector('ul  li:nth-child(2)')
      console.log(country.previousSibling) // 了解
      console.log(country.previousElementSibling) // 上一个兄弟
      console.log(country.nextElementSibling) // 下一个兄弟

删除元素

删除元素 元素.remove()

const ul = document.querySelector('ul')
      // ul.children[1].remove() '自杀' -> DOM树上不存在该元素
      // ul.removeChild(ul.children[1]) 父亲删除孩子
      // ul.children[1].style.display = 'none' // -> 元素隐藏,DOM树上还存在

      document.querySelector('button').addEventListener('click', function () {
        const r = confirm('你确定要删除吗?') // 提示确认框
        r && ul.children[1].remove()
      })

鼠标移入事件

mouseennter(推荐用) mouseover

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值