当前位置:   article > 正文

Web前端 Javascript笔记5

Web前端 Javascript笔记5

1、事件类型

①、HTML事件

事件说明
load当页面完全加载后在window上面触发,或当框架集加载完毕后在框架集上触发;
unload当页面完全卸载后在window上面触发,或当框架集卸载后在框架集上触发;
select当用户选择文本框(input或textarea)中的一个或多个字符触发;
change当文本框(input或textarea)内容改变且失去焦点后触发;
input输入;
focus当页面或者元素获得焦点时在window及相关元素上面触发;
blur当页面或元素失去焦点时在window及相关元素上触发;
submit当用户点击提交按钮在<form>元素上触发;
reset当用户点击重置按钮在<form>元素上触发;
resize当窗口或框架的大小变化时在window或框架上触发;
scroll当用户滚动带滚动条的元素时触发;

1、select

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. </head>
  6. <body>
  7. <textarea cols="30" rows="10"></textarea>
  8. <script>
  9. let b=document.querySelector("textarea")
  10. b.innerHTML="选择内容时候触发哈哈哈"
  11. b.addEventListener("select",()=>{
  12. b.style.backgroundColor="yellow"
  13. })
  14. </script>
  15. </body>
  16. </ht

2、change 

搭配使用文本框(input或textarea)

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. </head>
  6. <body>
  7. <textarea cols="30" rows="10"></textarea>
  8. <script>
  9. let b=document.querySelector("textarea")
  10. b.innerHTML="当输入内容改变且失去焦点后触发;"
  11. b.addEventListener("change",()=>{
  12. b.style.backgroundColor="yellow"
  13. })
  14. </script>
  15. </body>
  16. </ht

3、input

一输入就触发

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. </head>
  6. <body>
  7. <textarea cols="30" rows="10"></textarea>
  8. <script>
  9. let b=document.querySelector("textarea")
  10. b.innerHTML="输入"
  11. b.addEventListener("input",()=>{
  12. b.style.backgroundColor="yellow"
  13. })
  14. </script>
  15. </body>
  16. </ht

4、focus与blur

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. </head>
  6. <body>
  7. <textarea cols="30" rows="10"></textarea>
  8. <script>
  9. let b=document.querySelector("textarea")
  10. b.addEventListener("focus",()=>{
  11. b.style.backgroundColor="aqua"
  12. b.innerHTML="获得焦点"
  13. })
  14. b.addEventListener("blur",()=>{
  15. b.style.backgroundColor="yellow"
  16. b.innerHTML="失去焦点"
  17. })
  18. </script>
  19. </body>
  20. </html>

5、 submit与reset

注意

获取元素对象一定是<form action=""> </form>标签,提交与重置按钮一定也要包含在<form>标签中

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. <style>
  6. div{
  7. height: 100px;
  8. width: 200px;
  9. background-color: aqua;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <form action="">
  15. <input type="submit" value="提交">
  16. <input type="reset" value="重置">
  17. <div></div>
  18. </form>
  19. <script>
  20. const box=document.querySelector("div")
  21. const f = document.querySelector("form");
  22. f.addEventListener("submit",()=>{
  23. box.style.backgroundColor="yellow"
  24. })
  25. f.addEventListener("reset",()=>{
  26. box.style.backgroundColor="red"
  27. })
  28. </script>
  29. </body>
  30. </html>

6、resize尺寸大小事件 

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. <style>
  6. div{
  7. height: 100px;
  8. width: 200px;
  9. background-color: aqua;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div></div>
  15. <script>
  16. const box=document.querySelector("div")
  17. window.addEventListener("resize",()=>{
  18. box.style.backgroundColor="green"
  19. })
  20. </script>
  21. </body>
  22. </html>

7、scroll滚动事件

scrollTop\scrollLeft  获取元素向上滚出的高度,向左滚出的宽度

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. body {
  9. height: 3000px;
  10. width: 2000px;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <script>
  16. window.addEventListener("scroll", function () {
  17. console.log(document.documentElement.scrollTop)
  18. console.log(document.documentElement.scrollLeft)
  19. })
  20. </script>
  21. </body>

②、键盘事件

键盘事件最好与输入相关的文本元素或者输入元素搭配使用

事件说明
keydown当用户按下键盘上任意键时触发,如果按住不放,会重复触发;
keyup当用户释放键盘上的键触发;
keypress当按下并松开按键时触发

1、keydown与keyup

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. </head>
  6. <body>
  7. <textarea cols="30" rows="10"></textarea>
  8. <script>
  9. let b=document.querySelector("textarea")
  10. b.addEventListener("keydown",()=>{
  11. b.style.backgroundColor="blue"
  12. b.style.color="white"
  13. console.log("按下")
  14. })
  15. b.addEventListener("keyup",()=>{
  16. b.style.backgroundColor="pink"
  17. console.log("松开")
  18. })
  19. </script>
  20. </body>
  21. </html>

2、keypress

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. </head>
  6. <body>
  7. <textarea name="" id="" cols="30" rows="10"></textarea>
  8. <script>
  9. let b=document.querySelector("textarea")
  10. b.addEventListener("keypress",()=>{
  11. b.style.backgroundColor="green"
  12. b.style.color="white"
  13. console.log("按下并松开")
  14. })
  15. </script>
  16. </body>
  17. </html>

③、鼠标事件

事件说明
click单击鼠标按钮时触发
dblclick当用户双击主鼠标按钮时触发
mousedown当用户按下鼠标还未弹起时触发
mouseup当用户释放鼠标按钮时触发
mouseover当鼠标移到某个元素上方时触发
mouseout当鼠标移出某个元素上方时触发
mousemove当鼠标指针在元素上移动时触发
mouseenter在鼠标光标从元素外部首次移动至元素范围内触发,不参与冒泡
mouseleave鼠标移出

1、click

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. <style>
  6. div{
  7. height: 100px;
  8. width: 200px;
  9. background-color: aqua;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <button>点击</button>
  15. <div></div>
  16. <script>
  17. let box=document.querySelector("div")
  18. let btn=document.querySelector("button")
  19. btn.addEventListener("click",()=>{
  20. box.style.backgroundColor="yellow"
  21. })
  22. </script>
  23. </body>
  24. </html>

2、dblclick 

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. <style>
  6. div{
  7. height: 100px;
  8. width: 200px;
  9. background-color: aqua;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <button>点击</button>
  15. <div></div>
  16. <script>
  17. let box=document.querySelector("div")
  18. let btn=document.querySelector("button")
  19. btn.addEventListener("dblclick",()=>{
  20. box.style.backgroundColor="yellow"
  21. box.innerHTML="双击鼠标触发"
  22. })
  23. </script>
  24. </body>
  25. </html>

3、mousedown与mouseup

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>Document</title>
  6. <style>
  7. textarea{
  8. height: 200px;
  9. width: 300px;
  10. resize: both;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <textarea cols="40" rows="20"></textarea>
  16. <script>
  17. let t=document.querySelector("textarea")
  18. t.addEventListener("mousedown",()=>{
  19. t.style.backgroundColor="yellow"
  20. t.innerHTML="当用户按下鼠标还未弹起时触发"
  21. })
  22. t.addEventListener("mouseup",()=>{
  23. t.style.backgroundColor="yellowgreen"
  24. t.innerHTML="当用户释放鼠标按钮时触发"
  25. })
  26. </script>
  27. </body>

4、mouseenter与mouseleave

移进移出鼠标时,要点击才有效果

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>Document</title>
  6. <style>
  7. .dad{
  8. height: 280px;
  9. width: 280px;
  10. background-color: green;
  11. }
  12. .son{
  13. height: 150px;
  14. width: 150px;
  15. background-color: yellow
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div class="dad">
  21. <div class="son"></div>
  22. </div>
  23. <script>
  24. let t1=document.querySelector(".dad")
  25. let t2=document.querySelector(".son")
  26. t1.addEventListener("mouseenter",()=>{
  27. console.log("鼠标在元素上")
  28. })
  29. t1.addEventListener("mouseleave",()=>{
  30. console.log("鼠标移除元素外")
  31. })
  32. </script>
  33. </body>
  34. </html>

5、mouseover与mouseout

只要鼠标指针移入事件所绑定的元素或其子元素,都会触发该事件

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. <style>
  6. .dad{
  7. height: 280px;
  8. width: 280px;
  9. background-color: green;
  10. }
  11. .son{
  12. height: 150px;
  13. width: 150px;
  14. background-color: yellow
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div class="dad">
  20. <div class="son"></div>
  21. </div>
  22. <script>
  23. let t1=document.querySelector(".dad")
  24. let t2=document.querySelector(".son")
  25. t1.addEventListener("mouseover",()=>{
  26. console.log("鼠标在元素上")
  27. })
  28. t1.addEventListener("mouseout",()=>{
  29. console.log("鼠标移除元素外")
  30. })
  31. </script>
  32. </body>
  33. </html>

 6、mouseover ,mouseout ,mouseenter,mouseleave的区别

其实主要是mouseover与mouseout会引起冒泡,多次触发

7、mousemove

  1. <head>
  2. <style>
  3. div{
  4. height: 200px;
  5. width: 200px;
  6. background-color: rgb(136, 238, 136);
  7. }
  8. </style>
  9. </head>
  10. <body>
  11. <div></div>
  12. <script>
  13. let b=document.querySelector("div")
  14. b.addEventListener("mouseover",()=>{
  15. b.innerHTML="当鼠标指针在元素上移动时触发"
  16. b.style.backgroundColor="pink"
  17. })
  18. </script>


2、事件对象

  • 在Javascript中发生事件时,都会产生一个事件对象event,这个对象中包含着所有与事件相关的信息
  • 记录触发事件的所有记录,所有信息

//event.code键码值

对象.addEventListener("事件",(event或者e)=>{

           事件对象操作

            }

时间对象的使用可以让客户知道,在进行什么事件 

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. </head>
  6. <body>
  7. <textarea cols="30" rows="10"></textarea>
  8. <script>
  9. const txt=document.querySelector("textarea")
  10. //记录关于事件的所有信息
  11. txt.addEventListener("keydown",(event)=>{
  12. console.log(event)
  13. } )
  14. </script>
  15. </body>
  16. </html>

keydown事件, 在文本框输入按下键盘,控制台就出现了一次事件触发的具体信息

//事件由用户行为是否触发

isTrusted: true, 

//按键标识符

key: '1',

按键标识符是表示键盘按钮的字符串,该属性的返回值可以是:

  • 单个字母 (如 "a", "W", "4", "+" 或 "$")
  • 多个字母 (如 "F1", "Enter", "HOME" 或 "CAPS LOCK")

//键码值

code: 'Digit1',

//返回按键在设备上的位置

location: 0 

//用户的按键编码

keyCode: 49

//触发发生的元素容器

target: textarea

//事件类型

type: "keydown"

//以页面的左上角为原点。鼠标离浏览器可视区域左上角的距离,不随滚动条滚动而改变;

clientX: 135

clientY: 114

//鼠标位置相对于目标节点的位置

offsetX: 126

offsetY: 106

//以页面的左上角为原点。鼠标离浏览器可视区域左上角的距离,随滚动条滚动而改变;

pageX: 135

pageY: 114

//发生事件时,以显示器屏幕的左上角为原点,鼠标距离显示器屏幕左上角的坐标

screenX: 832

screenY: 239

//鼠标类型:小手

pointerType: "mouse"

  • altKey:是否按下Alt键
  • ctrlKey:是否按下Ctrl键
  • shiftKey:是否按下Shift键
  • metaKey:是否按下Meta键

  1. <body>
  2. <textarea cols="30" rows="10"></textarea>
  3. <script>
  4. const txt=document.querySelector("textarea")
  5. txt.addEventListener("keydown",(event)=>{
  6. console.log(event.code)
  7. if (event.key=="Enter"){
  8. console.log("回车键")
  9. }
  10. })
  11. </script>
  12. </body>

 利用事件,可以打印出你键盘敲下了什么


3、捕获与冒泡

Dom事件标准描述了事件传播的 3 个阶段:

  1. 捕获阶段(Capturing phase)—— 事件(从 Window)向下走近元素。
  2. 目标阶段(Target phase)—— 事件到达目标元素。
  3. 冒泡阶段(Bubbling phase)—— 事件从元素上开始冒泡。

可以用事件对象方法event.eventPhase()返回事件传播当前的阶段

冒泡:当一个事件发生在一个元素上,它会首先运行在该元素上的处理程序,然后运行其父元素上的处理程序,然后一直向上到其他祖先上的处理程序。

几乎所有事件都会冒泡。

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. <style>
  6. .grdad {
  7. width: 350px;
  8. height: 350px;
  9. background-color: aqua;
  10. }
  11. .dad {
  12. width: 250px;
  13. height:250px;
  14. background-color:yellow;
  15. }
  16. .son {
  17. width: 150px;
  18. height: 150px;
  19. background-color: red;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div class="grdad">
  25. <div class="dad">
  26. <div class="son"></div>
  27. </div>
  28. </div>
  29. <script>
  30. const g = document.querySelector(".grdad")
  31. const d = document.querySelector(".dad")
  32. const s = document.querySelector(".son")
  33. g.addEventListener("click", function (e) {
  34. console.log("爷爷触发")
  35. })
  36. d.addEventListener("click", function (e) {
  37. console.log("爸爸触发")
  38. })
  39. s.addEventListener("click", function (e) {
  40. console.log("儿子触发")
  41. })
  42. </script>
  43. </body>
  44. </html>

 也就是说:点击 .son,事件首先通过祖先链向下到达元素(捕获阶段),然后到达目标(目标阶段),最后上升(冒泡阶段),在途中调用处理程序。

  • 停止冒泡

用于停止冒泡的方法是 event.stopPropagation()。 

  1. g.addEventListener("click", function (e) {
  2. console.log("爷爷触发")
  3. e.stopPropagation()
  4. })
  5. d.addEventListener("click", function (e) {
  6. console.log("爸爸触发")
  7. e.stopPropagation()
  8. })
  9. s.addEventListener("click", function (e) {
  10. console.log("儿子触发")
  11. e.stopPropagation()
  12. })

我们在使用时间监听的时候,第三个参数决定冒泡

第三个参数选项有两个可能的值:

  • 如果为 false(默认值),则在冒泡阶段设置处理程序。
  • 如果为 true,则在捕获阶段设置处理程序。

比如:

要是事件监听第三个参数是true,盒子触发顺序会从爷爷开始,到父亲,最后到儿子

  1. g.addEventListener("click", function (e) {
  2. console.log("爷爷触发")
  3. },true)
  4. d.addEventListener("click", function (e) {
  5. console.log("爸爸触发")
  6. },true)
  7. s.addEventListener("click", function (e) {
  8. console.log("儿子触发")
  9. },true)


4、组止表单提交

  event.preventDefault()     :阻止默认行为

元素对象.addEventListener("click", function (e或者event) {

            e或者event.preventDefault()

        })

元素对象.on事件 =(e或者event) =>{

            e或者event.preventDefault()

        })

  1. <form action="">
  2. <input type="text" name="name="佳人们你们好我作业还没写完555啊"">
  3. <button>提交</button>
  4. </form>
  5. <script>
  6. const btn = document.querySelector("button")
  7. btn.addEventListener("click", function () {
  8. console.log("点击")
  9. </script>

 点击提交,山面的路径栏发生了变化

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. </head>
  6. <body>
  7. <form action="">
  8. <input type="text" name="佳人们你们好我作业还没写完555啊" >
  9. <button>提交</button>
  10. </form>
  11. <script>
  12. const b = document.querySelector("button")
  13. b.addEventListener("click", function (e) {
  14. e.preventDefault()
  15. })
  16. </script>
  17. </body>
  18. </html>

 点击提交,山面的路径栏发生了变化 


5、全选案例

  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. table,th,td{
  8. border: 3px solid grey;
  9. }
  10. table {
  11. width: 100px;
  12. margin: 100px auto;
  13. }
  14. th {
  15. height: 25px;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <table>
  21. <tr>
  22. <th>
  23. <input type="checkbox" class="a"> <span>全选</span>
  24. </th>
  25. </tr>
  26. <tr>
  27. <td>
  28. <input type="checkbox" class="c">
  29. </td>
  30. </tr>
  31. <tr>
  32. <td>
  33. <input type="checkbox" class="c">
  34. </td>
  35. </tr>
  36. <tr>
  37. <td>
  38. <input type="checkbox" class="c">
  39. </td>
  40. </tr>
  41. </table>
  42. <script>
  43. const a = document.querySelector(".a")
  44. const c = document.querySelectorAll(".c")
  45. a.onclick=()=>{
  46. if (a.checked == true) {
  47. for (let i = 0; i < c.length; i++) {
  48. c[i].checked = true
  49. }
  50. } else {
  51. for (let i = 0; i < c.length; i++) {
  52. c[i].checked = false
  53. }
  54. }
  55. }
  56. for (let i = 0; i < c.length; i++) {
  57. c[i].onclick=()=>{
  58. let b = document.querySelectorAll(".c:checked")
  59. if (b.length == c.length) {
  60. a.checked = true
  61. } else {
  62. a.checked = false
  63. }
  64. }
  65. }
  66. </script>
  67. </body>
  68. </html>

 


6、事件委托

利用默认存在的冒泡,委托父级元素获取多数相同的元素对象

可以使用事件对象属性even.target---->返回触发此事件的的元素(事件的目标节点)

通俗一点讲,就是获取我们点击的对象

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. </head>
  6. <body>
  7. <ul>
  8. <li>111</li>
  9. <li>222</li>
  10. <li>333</li>
  11. <li>444</li>
  12. </ul>
  13. <script>
  14. const ul=document.querySelector("ul")
  15. ul.onclick=(e)=>{
  16. e.target.style.backgroundColor="yellow"
  17. }
  18. </script>
  19. </body>
  20. </html>

 选中的变成黄色

  • 若是父级元素里的子元素不都是相同的,有混入其他元素,但是只想让特定的子元素被选中,该怎么办呢?
  • 使用tagName属性获取标签名 JavaScript提供了一个名为 tagName 的属性,可以用来获取元素的标签名。
  • 首先选中父级,后面交给父级的事件监听的事件对象属性去选中
  1. <body>
  2. <ul>
  3. <li>111</li>
  4. <li>222</li>
  5. <li>333</li>
  6. <li>444</li>
  7. <h3>佳人们你们好啊</h3>
  8. </ul>
  9. <script>
  10. const ul=document.querySelector("ul")
  11. ul.onclick=(e)=>{
  12. if(e.target.tagName ==="LI"){
  13. e.target.style.backgroundColor="blue"
  14. }
  15. }
  16. </script>
  17. </body>


7、client.offset

假如你想了解一个元素的宽和高,但是一些元素的宽高是由内容撑起来的,于是该如何获取宽高

  • 元素.clientWidth //获取元素自身的宽高,不包含边框、margin、滚动条
  • 元素.clientHeight
  • 元素.offsetWidth //获取元素自身的宽高,包含padding、border
  • 元素.offsetHeight

首先来个行内块元素,不设宽度,设高度,让他的宽度被内容撑大 

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. <style>
  6. div {
  7. display: inline-block;
  8. height: 100px;
  9. background-color: yellowgreen;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <div>撸起袖子加油炫</div>
  15. <script>
  16. const b = document.querySelector("div")
  17. console.log(b.clientWidth)
  18. console.log(b.clientHeight)
  19. </script>
  20. </body>
  21. </html>

 即使不设宽度,盒子被撑大的宽度也显现出来

div {

            display: inline-block;

            /* width: 200px; */

            height: 100px;

            background-color: yellowgreen;

            padding: 10px;

        }        让全局属性增加内边距

我们发现,宽度增加了20px,.client会含有内边距

增加了边框与外边距我们发现,宽度还是20px ,说明.client不含有边框与外边距

将js的输出换成:

  •         console.log(b.offsetWidth)
  •         console.log(b.offsetHeight)

div有内边距与边框

 div {

            display: inline-block;

            height: 100px;

            background-color: yellowgreen;

            border: 10px dashed green;

            padding: 10px;

        }

 我们可以看到,宽度增加了40,所以.offset包含边框与内边距


8、获取元素位置

获取自己与有定位的父级的左、上角距离

元素对象.offsetTop

元素对象.offsetLeft

1、 一个简单的盒子

  1. <style>
  2. div {
  3. width: 200px;
  4. height: 200px;
  5. background-color: palevioletred;
  6. }
  7. </style>
  8. <body>
  9. <div></div>
  10. <script>
  11. const b = document.querySelector("div")
  12. console.log(b.offsetTop)
  13. console.log(b.offsetLeft)
  14. </script>

距离页面左上角距离都是8,因为body默认内边距8px

2、盒子加了10pxd外边距

距离顶部是10(上下外边距与边距合并,以外边距大的为准),距离坐标变成了18不,外边距与左右边距可以合并

3、加一个没有定位的父盒子

我们可以看到距离啥都没变

4、让父盒子有定位

距离左边是10px,因为外边距,距离顶部0px,因为上下的外边距塌陷

9、创建节点

委托父节点,首先创造节点,然后追加节点内容,父节点添加创造的新节点

追加节点:

            // 父元素.appendChild(新的节点)

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. </head>
  6. <body>
  7. <button>点击添加li</button>
  8. <ul>
  9. <li>111</li>
  10. <li>222</li>
  11. <li>333</li>
  12. </ul>
  13. <script>
  14. const b = document.querySelector("button")
  15. const u = document.querySelector("ul")
  16. b.onclick=()=> {
  17. let lli=document.createElement("li")
  18. lli.innerHTML='我是新的li'
  19. u.appendChild(lli)
  20. }
  21. </script>
  22. </body>
  23. </html>

 除了在默认在后面添加,还可以在父级内选择下标插到想要插进的位置

父级元素.insertBefore(新的节点,追加的位置)

//追加节点改成这个

            u.insertBefore(lli,u.children[1])


10、克隆节点

元素对象.cloneNode(bool)                bool=true或者false

true表示会将后代节点一起克隆,默认false,文字也是节点

  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Document</title>
  5. </head>
  6. <body>
  7. <ul>
  8. <li>我是一个li</li>
  9. </ul>
  10. <script>
  11. const li =document.querySelector("ul li")
  12. const newl=li.cloneNode(true)
  13. console.log(newl)//如果克隆了,就从控制台输出
  14. </script>
  15. </body>
  16. </html>

控制台得到输出 

如果里面的布尔参数没有,默认为false,const newl=li.cloneNode()

获得的li没有文字


11、删除节点

父元素.removeChild(父元素,删除的位置)

        //删除的操作必须依靠父元素

  1. <body>
  2. <ul>
  3. <li>1</li>
  4. <li>2</li>
  5. <li>3</li>
  6. <li>4</li>
  7. </ul>
  8. <script>
  9. const ul = document.querySelector("ul")
  10. ul.removeChild(ul.children[2])
  11. </script>
  12. </body>


12、setTimeout()

        主要用来指定函数后者某段代码,在多长时间之后执行  .会返回一个整数,作为定时器的编号,方便后期清除定时器

  • setTimeout(function或者code,time)-->用户未对第二个参数进行传参,则默认是0
  • 本质上是-->window.setTimeout(function或者code)

计时器编号与两种定义方式:

  1. <script>
  2. let t = setTimeout(()=>{
  3. 'console.log("哈哈哈佳人们好困")'
  4. }, 3000)
  5. console.log(t)
  6. let t1 = setTimeout('console.log("你们看到这句话的时候我早已身价百万!!")', 4000)
  7. console.log(t1)
  8. </script>

 3秒,四秒后出来的东西,出现前会返回计时器编号

清除计时器 :

  1. <script>
  2. let t = setTimeout('console.log("你们看到这句话的时候我早已身价百万!!")', 4000)
  3. console.log(t)
  4. clearTimeout(t)
  5. </script>

清除计时器后,控制台除了返回一个定时器编号之外啥都没有出现 

 以函数为参数:

  1. function t2() {
  2. console.log("好了烦躁冈抑云")
  3. }
  4. window.setTimeout(t2)

 没有返回定时器编号

可以传参数:

  1. setTimeout(function (a, b, c) {
  2. console.log(a + b*c)
  3. }, 3000, 3, 2, 3)

关于this

        // 在全局作用域下,this关键字指向的是window

        // 对象中的this,默认指向对象本身

        // 箭头函数没有this的作用域

  1. <script>
  2. setTimeout(function (a) {
  3. console.log(this)
  4. }, 3000, 3)
  5. console.log(this)
  6. </script>

  1. x = 111
  2. let obj = {
  3. x: 2,
  4. sing: function () {
  5. console.log(this.x)
  6. }
  7. }
  8. obj.sing()
  9. setTimeout(obj.sing.bind(obj), 3000)


13、setInterva()

每个一段时间执行代码

  1. // 每个一段时间执行一次代码
  2. let t= setInterval(function () {
  3. console.log("hello,setInterval")
  4. }, 1500)
  5. console.log(t)

可以传参:

let t2 = setInterval(function (a, b) {

            console.log("喀喀喀")

            console.log(a)

            console.log(b)

        }, 1000, 9, 5)

        console.log(t2)

清除计时器:

        clearInterval(t2)

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

闽ICP备14008679号