当前位置:   article > 正文

web前端第三次笔记

web前端第三次笔记

1事件类型

  1. 获取焦点事件
  2. ipt.addEventListener("focus", () => {
  3. .log("")
  4. })
  5. 失去焦点事件
  6. ipt.addEventListener("blur", () => {
  7. console.log("")
  8. })
  9. 文本输入事件
  10. txt.addEventListener("input", () => {
  11. console.log("")
  12. })
  13. 文本改变事件
  14. txt.addEventListener("change", () => {
  15. console.log("")
  16. })
  17. 鼠标移入事件
  18. txt.addEventListener("mouseenter", () => {
  19. txt.style.backgroundColor = ""
  20. })
  21. 鼠标移出事件
  22. txt.addEventListener("mouseleave", () => {
  23. txt.style.backgroundColor = ""
  24. })
  25. 键盘按下事件
  26. txt.addEventListener("keydown", () => {
  27. console.log("")
  28. })
  29. 键盘弹开事件
  30. txt.addEventListener("keyup", (a) => {
  31. console.log("")
  32. })
  33. 页面加载事件
  34. window.addEventListener("load", function () {
  35. const btn = document.querySelector("button")
  36. btn.addEventListener("click", () => {
  37. console.log("")
  38. })
  39. })
  40. 滚动事件 (scrollLeft 获取元素向左滚出的高度 scrollTop 获取元素向上滚出的高度)
  41. window.addEventListener("scroll", function () {
  42. console.log("")
  43. })
  44. 尺寸事件
  45. window.addEventListener("resize", () => {
  46. console.log("")
  47. })
  48. 捕获 冒泡事件(true 捕获;false 冒泡)
  49. const gf = document.querySelector(".grandFather")
  50. const f = document.querySelector(".father")
  51. const s = document.querySelector(".son")
  52. gf.addEventListener("click", function (e) {
  53. console.log("我是爷爷触发的事件")
  54. e.stopPropagation()
  55. })
  56. f.addEventListener("click", function (e) {
  57. console.log("我是爸爸触发的事件")
  58. e.stopPropagation()
  59. })
  60. s.addEventListener("click", function (e) {
  61. console.log("我是儿子触发的事件")
  62. e.stopPropagation()
  63. })

 2阻止表单提交

  1. const btn = document.querySelector("button")
  2. btn.addEventListener("click", function (e) {
  3. e.preventDefault()
  4. })

3事件委托

  1. const ul = document.querySelector("ul")
  2. ul.addEventListener("click", function (e) {
  3. if (e.target.tagName === "LI") {
  4. e.target.style.backgroundColor = "red"
  5. }
  6. })

4获取元素位置

  1. const box = document.querySelector("div")
  2. const p = document.querySelector("p")
  3. console.log(p.offsetTop)
  4. console.log(p.offsetLeft)

5创建节点

  1. const btn = document.querySelector("button")
  2. const ul = document.querySelector("ul")
  3. btn.addEventListener("click", function () {
  4. const newLi = document.createElement("li")
  5. })

6给新节点加入内容

  1. btn.addEventListener("click", function () {
  2. const newLi = document.createElement("li")
  3. newLi.innerHTML = ``
  4. })

 7追加节点到指定位置

  1. btn.addEventListener("click", function () {
  2. const newLi = document.createElement("li")
  3. newLi.innerHTML = ``
  4. ul.insertBefore(newLi, ul.children[0])
  5. })

 8克隆节点

  1. const li = document.querySelector("ul li")
  2. const newli = li.cloneNode(true)
  3. console.log(newli)
  4. cloneNode(true) 表示克隆时将后代节点一起克隆,默认情况下为“false”

9删除节点

  1. const ul = document.querySelector("ul")
  2. ul.removeChild(ul.children[1])
  3. 必须基于父元素进行删除

10setTimeout

  1. let timer2 = setTimeout('console.log("Hello Word")', 3000)
  2. clearTimeout(timer2)
  3. 用来指定函数某段代码,在一定时间之后执行,返回一个整数,作为定时器的编号,以此后期清除定时器
  4. 根据定时器返回的整数清除定时器;在全局作用域下,this关键字指向window;对象中的this,默认指向对象本身;箭头函数没有this的作用域

11setInterval  clearInterval

  1. let timer = setInterval(function (a, b) {
  2. console.log("hello word")
  3. console.log(a)
  4. console.log(b)
  5. }, 1000, 1, 2)
  6. console.log(timer)
  7. 每过一段时间运行一次代码
  8. let timer = setInterval(function (a, b) {
  9. console.log("hello word")
  10. console.log(a)
  11. console.log(b)
  12. }, 1000, 1, 2)
  13. console.log(timer)
  14. clearInterval(timer)
  15. 用来停止setInterval()方法执行的函数代码

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/515147
推荐阅读
相关标签
  

闽ICP备14008679号