onload事件

先看为什么会需要用到onload事件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script>
  7. var ele = document.querySelector(".c1");
  8. console.log(ele);
  9. </script>
  10. </head>
  11. <body>
  12. <div class="c1">mf</div>
  13. </body>
  14. </html>

页面默认从上至下加载,如上案例先加载script部分,但是i1还未加载完毕没,所以打印null。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script>
  7. window.onload = function (){
  8. ele = document.getElementById("i1")
  9. console.log(ele.innerHTML);
  10. }
  11. </script>
  12. </head>
  13. <body>
  14. <div id="i1">mf</div>
  15. </body>
  16. </html>

等待document即页面所有DOM元素加载完,才执行。

onsubmit事件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script>
  7. window.onload = function () {
  8. var ele = document.querySelector(".c1");
  9. console.log(ele);
  10. };
  11. </script>
  12. </head>
  13. <body>
  14. <form action="" id="i1">
  15. <input type="text" name="user" class="c1">
  16. <input type="submit">
  17. </form>
  18. <script>
  19. var form = document.querySelector("#i1");
  20. var user = document.querySelector(".c1");
  21. form.onsubmit = function (e) {
  22. // alert(123)
  23. if (user.value.length<5){
  24. alert("用户名的长度应该大于等于5!");
  25. user.value = "";
  26. // 阻止默认的提交事件
  27. // 方式1:
  28. // return false;
  29. // 方式2:
  30. // e.preventDefault()
  31. }
  32. }
  33. </script>
  34. </body>
  35. </html>

onchange事件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <select name="" id="provice">
  9. <option value="">请选择省份</option>
  10. <option value="hubei">湖北省</option>
  11. <option value="hebei">河北省</option>
  12. <option value="guangdong">广东省</option>
  13. </select>
  14. <select name="" id="city">
  15. <option value="">请选择城市</option>
  16. </select>
  17. <script>
  18. var data = {"hubei":["襄阳","武汉","荆州"],"hebei":["保定","石家庄","廊坊"],"guangdong":["深圳","广州","惠州"]}
  19. var provice = document.querySelector("#provice");
  20. var city = document.querySelector("#city");
  21. provice.onchange = function () {
  22. console.log(this.value);
  23. var cities = data[this.value];
  24. console.log(cities);
  25. //清空方法
  26. city.options.length = 1;
  27. for (var index in cities){
  28. console.log(cities[index]);
  29. // 创建标签
  30. var option = document.createElement("option");
  31. option.innerHTML = cities[index];
  32. city.appendChild(option);
  33. }
  34. }
  35. </script>
  36. </body>
  37. </html>

onmouse事件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. .c1{
  8. width: 300px;
  9. height: 300px;
  10. background-color: orange;
  11. }
  12. .container{
  13. width: 300px;
  14. }
  15. .title{
  16. height: 50px;
  17. background-color: lightgray;
  18. }
  19. .container .list .item1{
  20. height: 50px;
  21. background-color: gold;
  22. }
  23. .container .list .item2{
  24. height: 50px;
  25. background-color: green;
  26. }
  27. .container .list .item3{
  28. height: 50px;
  29. background-color: #369;
  30. }
  31. .hide{
  32. display: none;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <div class="c1"></div>
  38. <div class="container">
  39. <div class="title">标题</div>
  40. <div class="list hide">
  41. <div class="item1">item1</div>
  42. <div class="item2">item2</div>
  43. <div class="item3">item3</div>
  44. </div>
  45. </div>
  46. <script>
  47. var ele = document.querySelector(".c1");
  48. ele.onmouseover = function () {
  49. this.style.backgroundColor = "red";
  50. };
  51. ele.onmouseleave = function f() {
  52. this.style.backgroundColor = "orange";
  53. };
  54. var title = document.querySelector(".title");
  55. var list = document.querySelector(".list");
  56. var container = document.querySelector(".container");
  57. title.onmouseover = function () {
  58. list.classList.remove("hide");
  59. };
  60. container.onmouseleave = function () {
  61. list.classList.add("hide");
  62. }
  63. </script>
  64. </body>
  65. </html>

onkey事件

  1. <input type="text" id="t1"/>
  2. <script type="text/javascript">
  3. var ele=document.getElementById("t1");
  4. ele.onkeydown=function(e){
  5. console.log("onkeydown",e.key)
  6. };
  7. ele.onkeyup=function(e){
  8. console.log("onkeyup",e.key)
  9. };
  10. </script>

onblur和onfocus事件

  1. <input type="text" class="c1">
  2. <script>
  3. var ele = document.querySelector(".c1");
  4. // 获取焦点事件
  5. ele.onfocus = function () {
  6. console.log("in")
  7. };
  8. // 失去焦点事件
  9. ele.onblur = function () {
  10. console.log("out")
  11. }
  12. </script>

冒泡事件

  1. <div class="c1">
  2. <div class="c2"></div>
  3. </div>
  4. <script>
  5. var ele1 = document.querySelector(".c1");
  6. ele1.onclick = function () {
  7. alert("c1区域")
  8. };
  9. var ele2 = document.querySelector(".c2");
  10. ele2.onclick = function (event) {
  11. alert("c2区域");
  12. // 如何阻止事件冒泡
  13. event.stopPropagation();
  14. }
  15. </script>