Blogger Information
Blog 47
fans 0
comment 0
visits 21094
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS事件的相关特性以及原理
P粉036614676
Original
311 people have browsed it

1.class对象

目标1:
1.改变H1标签的class属性
2.classList对象的添加,判断,替换,删除

  1. <!DOCTYPE html>
  2. <html lang="en">
  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. </head>
  9. <body>
  10. <h1 >改变</h1>
  11. <script>
  12. let h1 = document.querySelector('h1');
  13. h1.className = 'red';
  14. h1.classList.add('blue');
  15. h1.classList.remove('red');
  16. if(h1.classList.contains('blue'))
  17. {
  18. h1.classList.replace('blue','red');
  19. }
  20. h1.classList.toggle('red');
  21. console.log(h1.className);
  22. </script>
  23. </body>
  24. </html>

// 总结:
// 1.在JS中,Element的class叫className,可以直接用这个属性改变
// 也可以用【Element】.classList;
// 2.【Element】.classList.add();【Element】.classList.remove()
// 【Element】.classList.contains();【Element】.classList.replace();
// 【Element】.classList.toggle()

2.事件的添加与删除

目标1:
1.事件对象的使用(设置和取消)
2.设置监听器(设置和取消)
3.搞懂冒泡
目标2:
1.事件派发(自定义事件,事件触发,定时器(一次性,定时性))
2.限制

  1. <!DOCTYPE html>
  2. <html lang="en">
  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>元素对象</button>
  11. <button>元素事件监听器</button>
  12. <button>事件派发器/广告联盟点击赚钱</button>
  13. <script>
  14. let btn1 = document.querySelector('button:first-of-type');
  15. btn1.onclick = () => {
  16. console.log(event);
  17. }
  18. btn1.onclick = null;
  19. const f = function () {
  20. console.log(event);
  21. }
  22. let btn2 = document.querySelector('button:nth-of-type(2)');
  23. btn2.addEventListener('click',f,false);//函数不用‘’
  24. btn2.removeEventListener('click',f,false);
  25. // 目标2:
  26. // 1.事件派发(自定义事件,事件触发,定时器(一次性,定时性))
  27. // 2.限制
  28. let btn3 = document.querySelector('button:nth-of-type(3)');
  29. let i = 0;
  30. let y = () => {
  31. console.log('你以及转了: ' + i + '元');
  32. i++;
  33. }
  34. btn3.addEventListener('click',y,false);
  35. const myEvent = new Event('click');
  36. // btn3.dispatchEvent(myEvent);
  37. // setTimeout(() => {btn3.dispatchEvent(myEvent);},2000)
  38. //定时触发
  39. //间歇触发
  40. let time = setInterval(() => {
  41. btn3.dispatchEvent(myEvent);
  42. if(i>=10)
  43. {
  44. clearInterval(time);
  45. }
  46. },1000)

总结:
1.事件添加:
1.1直接添加(Element.事件 = ‘’)
解除:Element.事件 = null
1.2添加监听(Element.addEventListener(‘事件’,触发函数,false))
解除:Element.removeEventListener(‘事件’,触发函数,false)
2.事件派发
2.1添加事件
2.2事件触发(Element.dispatchEvent(事件))
2.3定时触发(setTimeout(函数,时间))
2.4间歇触发,以及停止(setInterval(函数,时间)),clearInterval

3.事件传递机制与事件冒泡/事件代理/委托

目标1:
1.事件对象
2.事件主体
3.事件目标

目标2:
1.冒泡传递
2.阻止冒泡
3.target和currenttarget的区别

  1. <!DOCTYPE html>
  2. <html lang="en">
  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:
  11. 1.事件对象
  12. 2.事件主体
  13. 3.事件目标 -->
  14. <button onclick="show()">点击</button>
  15. <ul>
  16. <li data-index="1">item1</li>
  17. <li data-index="2">item2</li>
  18. <li data-index="3">item3</li>
  19. <li data-index="4">item4</li>
  20. <li data-index="5">item5</li>
  21. </ul>
  22. <script>
  23. let show = () => {
  24. console.log(event);
  25. console.log(event.currentTarget);
  26. console.log(event.target);
  27. //event:触发这个事件的所有操作
  28. }
  29. let ul = document.querySelector('ul');
  30. ul.onclick = () => {
  31. console.log(event.currentTarget);
  32. }
  33. const items = document.querySelectorAll('li');
  34. items.forEach(element => {
  35. element.onclick = () => {console.log(event.currentTarget,event.target);
  36. event.stopPropagation();//阻止冒泡
  37. }
  38. });
  39. document.querySelector('button');
  40. </script>
  41. </body>
  42. </html>

4.表单控件

目标1:
1.禁止按钮提交默认行为
2.禁止冒泡

  1. 3. 表单非空验证

```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>表单控件</title>
</head>

<body>
<!-- 2. 取消form表单元素的默认提交行为 -->
<!-- <form action="login.php" method="post" id="login" onsubmit="return false"> -->
<form action="login.php" method="post" id="login">
<label class="title">用户登录</label>
<label for="email">邮箱:</label>
<input type="email" id="ema" name="email" value="" autofocus />
<label for="password">密码:</label>
<input type="password" id="password" name="password" />
<!-- button: 如果写到form标签内, 默认是提交行为 -->
<!-- 1. 修改button 类型 -->
<!-- <button type="button" name="submit" onclick="check(this)">登录</button> -->
<button name="submit" onclick="check(this)">登录</button>
</form>
<script>
function check(els){
event.preventDefault();
event.stopPropagation();

  1. // console.log(els.form);
  2. // console.log(els.form.ema);
  3. // console.log(els.form.password); //根据id来的
  4. if(els.form.ema.value.length == 0)
  5. {
  6. alert('输入不能为空');
  7. els.form.ema.focus();
  8. return false;
  9. }
  10. if(els.form.password.value.length == 0)
  11. {
  12. alert('输入不能为空');
  13. els.form.password.focus();
  14. //聚集体会
  15. return false;
  16. }
  17. }
  18. </script>

</body>
</html>
总结:
1.禁止按钮提交默认行为:event.preventDefault();
2.禁止冒泡:event.stopPropagation();
3.获取表单元素的两种方式:
3.1直接获取
3.2【input】.form.

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:注意markdown 的代码块的格式,博文写好可以提前预览一下
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