Blogger Information
Blog 46
fans 2
comment 0
visits 19153
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1. 事件冒泡,事件代理的原理是什么,实例演示 2. 自选5个课堂中未提及的字符串API,进行演示
P粉314265155
Original
323 people have browsed it

classList对象与事件添加方式

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>classList对象与事件添加方式</title>
  8. <style>
  9. .red{
  10. color: red;
  11. }
  12. .bgc {
  13. background-color: blue ;
  14. }
  15. .ye{
  16. color: yellow;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <h1 class="red bgc ">hello</h1>
  22. <h2>word</h2>
  23. <script>
  24. // 1、style 属性:标签的行内样式
  25. // 2、getComputerStyle 计算样式
  26. // 3、classList 来操作 class 属性
  27. const h1 = document.querySelector('h1');
  28. console.log(h1.className);
  29. console.log(h1.classList);
  30. const h2 =document.querySelector('h2');
  31. console.log('================');
  32. // classList 的添加、 判断 替换 删除、切换
  33. // 1、add(); 添加
  34. h2.classList.add('red');
  35. // 2、contains 判断 输出布尔
  36. console.log( h2.classList.contains('red'));
  37. // 3、replace 替换
  38. h1.classList.replace('red','ye');
  39. // 4、remove 删除 '
  40. h1.classList.remove('bgc');
  41. // 5、切换
  42. // 如果有这个类就去掉,没有就加上
  43. console.log(h2.classList);
  44. console.log(h1.classList);
  45. // 有去掉
  46. h1.classList.toggle('ye');
  47. // 没有加上替换
  48. h2.classList.toggle('ye');
  49. </script>
  50. </body>
  51. </html>

事件的添加与删除

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>事件的添加与删除</title>
  8. </head>
  9. <body>
  10. <!-- <button onclick="alert(this.texcontent)">元素对象</button>
  11. -->
  12. <button>我是第一个按钮</button>
  13. <button>我是第三个按钮</button>
  14. <button>我是第四个按钮事件派发器</button>
  15. <button>我是第五个按钮事件派发器</button>
  16. <button>我是第二个按钮</button>
  17. <script>
  18. // 事件三要素
  19. // 1、事件名称 :字符串 click keydown scroll
  20. // 2、事件主体: 元素 button div p
  21. // 3、事件方法 ==事件侦听器 ==函数 function(){。。。。。。}
  22. const btn1 = document.querySelector('button:first-of-type');
  23. // 1、事件添加 的方法。事件监听
  24. // on +事件类型 type = callback 回调
  25. btn1.onclick = function () {
  26. // event:预定义对象。保存了绑定到当前事件主体上的事件对象详情 事件对象
  27. // event 官方已不建议使用 此方法 。事件对象。保存事件细节。不需要定义
  28. console.log('1');
  29. console.log(event);
  30. };
  31. // 简化 箭头函数
  32. // btn1.onclick = () => console.log(event);
  33. // 事件取消
  34. // btn1.onclick = null;
  35. // 2、事件监听 addEventListener()
  36. const btn2 = document.querySelector('button:last-of-type') ;
  37. // addEventListener() 事件名称.事件回调方法,触发阶段
  38. btn2.addEventListener('click',function(){
  39. console.log(event);
  40. },false);
  41. // 事件取消 当使用了事件监听器时,如果需要取消事件,就不能用回调来声明事件方法
  42. // btn2.removeEventListener('click',show,false);
  43. console.log('=======');
  44. const show =function(){
  45. console.log(event);
  46. }
  47. const btn3 = document.querySelector('button:nth-of-type(2)') ;
  48. // addEventListener() 事件名称.事件回调方法,触发阶段
  49. btn3.addEventListener('click',show,false);
  50. // 事件取消 当使用了事件监听器时,如果需要取消事件,就不能用回调来声明事件方法
  51. btn3.removeEventListener('click',show,false);
  52. // 3、事件派发
  53. // 第一步 :先创建一个自定义事件
  54. const btn4 = document.querySelector('button:nth-of-type(3)') ;
  55. let i = 0;
  56. btn4.addEventListener('click',function(){
  57. i=i+1;
  58. // console.log('已经赚了:'+ i + ' 元');
  59. // while(i > 10) return console.log('hello');;
  60. (i <10 )? (console.log('已经赚了:'+ i + ' 元')) :false;
  61. // if(i>= 100) return false;
  62. });
  63. console.log('--------');
  64. // 创建一个自定义事件
  65. const btn5 = document.querySelector('button:nth-of-type(4)') ;
  66. let h = 0;
  67. btn5.addEventListener('click', function () {
  68. h+= 1;
  69. console.log('已经赚了 : ' + h + ' 元');
  70. });
  71. const myEvent =new Event('click');
  72. // 事件派发// dispatchEvent
  73. btn5.dispatchEvent(myEvent);
  74. // 第二步:将事件自动派发给 指定元素,并触发
  75. // 添加定时器,自动派发setTimeout 单次执行,每个一段时间,执行一次,一次性的
  76. let timer= setInterval(function(){
  77. console.log('hello');
  78. btn5.dispatchEvent(myEvent);
  79. // 关闭定时器
  80. // h =1 ;
  81. if (h >= 10) clearInterval(timer);
  82. },2000
  83. );
  84. // setInterval 间歇性的,不间断的
  85. </script>
  86. </body>
  87. </html>

事件代理、事件传递、事件委托、事件传递机制与事件冒泡

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>事件代理、事件传递、事件委托、事件传递机制与事件冒泡
  8. </title>
  9. </head>
  10. <body>
  11. <button onclick="show()"> 点击</button>
  12. <ul>
  13. <li >item1</li>
  14. <li>item2</li>
  15. <li>item3</li>
  16. <li>item4</li>
  17. <li>item5</li>
  18. </ul>
  19. <ul>
  20. <li data-index="6" >item6</li>
  21. <li data-index="7" >item7</li>
  22. <li data-index="8" >item8</li>
  23. <li data-index="9" >item9</li>
  24. <li data-index="10" >item10</li>
  25. </ul>
  26. <script>
  27. // function show (){
  28. // // 事件独享:总是可用
  29. // console.log(event);
  30. // // 1、事件主体、触发者 代表当前事件触发的 button
  31. // console.log('事件主体:',event.currentTarget);
  32. // console.log(typeof event);
  33. // // console.log(typeof event.bubbles);
  34. // // 事件目标、
  35. // console.log('事件目标:',event.target);
  36. // 没有事件委托 时 事件主体= 事件目标 true 有事件委托是 就是fasle
  37. // console.log(event.currentTarget==event.target);
  38. // }
  39. // // 获取所有的li
  40. // const items =document.querySelectorAll('li');
  41. // // items.forEach((items) =>(items.onclick = ()=>console.log(event.currentTarget)));
  42. // items.forEach(function(items){
  43. // items.onclick = function(){
  44. // // 事件主体
  45. // console.log(event.currentTarget);
  46. // // 事件主体的内容
  47. // console.log(event.currentTarget.textContent);
  48. // // 阻止冒泡
  49. // event.stopPropagation();
  50. // }
  51. // });
  52. // // 1、每个li 的父级都是同一个,是ul
  53. // // 2| ul 的父级是body
  54. // // body的父级是 html
  55. // // 事件冒泡,1、当前元素的事件会向上传递,往父级上冒泡 2、
  56. // // 2、当父级的同名事件时会自动触发
  57. // // 关键词 :父级 、同名(事件名称)
  58. // // 点击li 触发ul 的点击事件
  59. // document.querySelector('ul').onclick = ( ) =>console.log(event.currentTarget);
  60. // // 触发ul 的点击事件 触发,引起ul 父级触发
  61. // document.querySelector('body').onclick = ( ) =>console.log(event.currentTarget);
  62. // // 触发body 的点击事件 触发,引起html 父级触发
  63. // document.querySelector('html').onclick = ( ) =>console.log(event.currentTarget);
  64. // // 触发html 的点击事件 触发,引起document 父级触发
  65. // document.onclick = ( ) =>console.log(event.currentTarget);
  66. // // 触发document 的点击事件 触发,引起windows 父级触发
  67. // window.onclick = ( ) =>console.log(event.currentTarget);
  68. // 简化版
  69. // 根据冒泡原理,当前元素上的同名事件,会向上传递到它的父级
  70. // 如果当前是一个列表,或者当前是一组集合,那么添加事件会非常麻烦
  71. // 不管当前有多少个元素,根据dom树的特点,都会有一个父级
  72. // 所以可将这个事件,直接添加到它的父级上即可,可以简化代码。但是
  73. // 需要通过一些手段判断哪个子元素触发了事件、
  74. // 事件委托、事件代理 :将子元素的事件委托到父元素上触发,以简略代码
  75. // 1、先获取到 所有 li的父级 ul
  76. const ul = document.querySelector('ul:last-of-type') ;
  77. // 1、给ul 添加事件
  78. ul.onclick = function(){
  79. // 事件目标 点击事件的触发者 li 子元素
  80. console.log(event.target);
  81. // 找到当前事件的触发者 可以为他添加自定义属性 data-index
  82. console.log(event.target.dataset.index);
  83. console.log('========');
  84. // 事件主体 onclick 父元素
  85. console.log(event.currentTarget);
  86. // 在事件代理的场景下,事件目标与事件主体是不一致: 父子关系
  87. console.log(event.target === event.currentTarget);
  88. }
  89. </script>
  90. </body>
  91. </html>

表单事件

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>表单事件</title>
  8. </head>
  9. <body>
  10. <!-- 1、添加 onsubmit="alert('hello') 事件,当提交时,跳转弹框 -->
  11. <!-- <form action="ph.php" method="post" id="login" onsubmit="alert('hello')"> -->
  12. <!-- 方式二 取消form表单元素的默认提交行为 -->
  13. <!-- <form action="ph.php" method="post" id="login" onsubmit="return false"> -->
  14. <form action="ph.php" method="post" id="login" >
  15. <label class="title">用户登录</label>
  16. <br>
  17. <label for="email">邮箱账户:</label>
  18. <input type="email" id="email" name="email" value="" autofocus />
  19. <br>
  20. <label for="password">邮箱密码:</label>
  21. <input type="password" name="password" id="password" autofocus />
  22. <br>
  23. <!-- 如果把<button 放到表单 form 里面默认就是提交行为 ,需要禁用掉 -->
  24. <!-- 1、修改 <button 类型 改为 button 不会跳转 页面-->
  25. <!-- <button type="button" name="submit" onclick="check(this)">登录</button> -->
  26. <button name="submit" onclick="check(this)">登录</button>
  27. </form>
  28. <script>
  29. function check(e) {
  30. // 方式三、禁用提交按键的 默认行为 ,增加用户提交信息验证 推荐
  31. event.preventDefault();
  32. // 防止 冒泡
  33. event.stopPropagation();
  34. // 进行非空验证
  35. // 每一个表单控件 input button select 都有一个属性 form 是当前表单的引用对象
  36. console.log(e.form);
  37. let email = e.form.email;
  38. let password = e.form.password;
  39. // 开始判断
  40. if (email.value.length ==0){
  41. alert('邮箱不能为空')
  42. // 焦点重置
  43. email.focus();
  44. return false;
  45. } else if (password.value.length ==0){
  46. alert('密码不能为空')
  47. password.focus();
  48. return false;
  49. } else {
  50. // document.textContent('登录成功');
  51. console.log('登录成功');
  52. }
  53. }
  54. // 1. submit: 提交
  55. // 2. focus: 焦点
  56. // 3. blur: 失去焦点
  57. // 4. change: 值改变,且失去焦点时
  58. // 5. input: 值一旦改变就触发, 不等失去焦点
  59. </script>
  60. </body>
  61. </html>

字符串中的常用

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>字符串中的常用</title>
  8. </head>
  9. <body>
  10. <script>
  11. let str = 'php中文网';
  12. console.log(str);
  13. console.log(str[1]);
  14. console.log(str.length);
  15. // charAt(1) 注意是小括号 根据索引拿到值
  16. console.log(str.charAt(1));
  17. // 根据值拿到索引 indexOf()
  18. console.log(str.indexOf('h'));
  19. console.log(str.search('h'));
  20. // 字符串拼装
  21. console.log(str.concat('('+'www.baidu.com'+')'));
  22. // 模板字面量 拼装 用反引号 括起来
  23. // 字符串替换 临时替换输出 不是最终的 ,但是可以保存到变量中
  24. console.log(str.replace('中文','百度'));
  25. console.log(str);
  26. // 输出 结果从 索引 0到 3 但是 不包括 3
  27. console.log(str.slice(0,3));
  28. // 第二个参数 是获取的数量
  29. console.log(str.substr(0,5));
  30. // 支持负数 从左到右 分别是 -1 -2 -3
  31. console.log(str.substr(-2,5));
  32. // -2 是从右边开始 的起始索引
  33. // 5是获取的数量,向右
  34. </script>
  35. </body>
  36. </html>

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!