image.png

    image.png

    1. ** onload静态事件**
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>事件</title>
    6. </head>
    7. <body onload="alert('onload事件执行')">
    8. <div></div>
    9. </body>
    10. </html>
    1. ** onload要执行多个静态事件**
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>事件</title>
    6. <script>
    7. function onloadFun(){
    8. alert('onload事件执行');
    9. }
    10. </script>
    11. </head>
    12. <body onload="onloadFun()">
    13. <div></div>
    14. </body>
    15. </html>
    1. ** onload动态事件注册**
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>onload动态注册</title>
    6. <script>
    7. window.onload=function () {
    8. alert("onload动态事件注册")
    9. }
    10. </script>
    11. </head>
    12. <body>
    13. </body>
    14. </html>
    1. ** onclick静态单击事件**
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>onclick静态注册</title>
    6. <script>
    7. function onclickFun() {
    8. alert("单击静态注册")
    9. }
    10. </script>
    11. </head>
    12. <body>
    13. <button onclick="onclickFun()">按钮1</button>
    14. </body>
    15. </html>
    1. <br /> ** onclick动态单击注册**
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>onclick动态注册</title>
    6. <script>
    7. window.onclick=function () {
    8. //先通过ID获取标签对象
    9. var obj=document.getElementById("001");
    10. //再通过对象.事件名=function(){}
    11. obj.onclick=function () {
    12. alert("动态单击事件注册");
    13. }
    14. }
    15. </script>
    16. </head>
    17. <body>
    18. <button id="001">按钮1</button>
    19. </body>
    20. </html>