Detailed explanation of custom objects in JS plus events in js

Custom Objects in JS

A custom object of the form Object

Object definition:

​ var variable name = new Object(); // object instance (empty object)

​ variable name. attribute name = value; // define an attribute

​ variable name. function name = function(){} // define a function

Object access:

​ variable name.property/function name();

<!DOCTYPE html>
        <html lang="en">
            <head>
                <meta charset="UTF-8">
            <title>Title</title>

         <script type="text/javascript">
            // 对象的定义:
            // var 变量名 = new Object(); // 对象实例(空对象)
            // 变量名.属性名 = 值; // 定义一个属性
            // 变量名.函数名 = function(){} // 定义一个函数
        var obj = new Object();
        obj.name = "华仔";
        obj.age = 18;
        obj.fun = function () {
      
      
        alert("姓名:" + this.name + " , 年龄:" + this.age);
    }
        // 对象的访问:
        // 变量名.属性 / 函数名();
        // alert( obj.age );
        obj.fun();
        </script>
    </head>
    <body>
    </body>
</html>

Custom objects in {} curly braces

Object definition:

​ var variable name = { // empty object

​ Attribute name: value, // define an attribute

​ Attribute name: value, // define an attribute

​ Function name: function(){} // define a function

​ };

​ Object access:

​ variable name.property/function name();

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">

<title>Title</title>

    <script type="text/javascript">
    // 对象的定义:
    // var 变量名 = { // 空对象
    // 属性名:值, // 定义一个属性
    // 属性名:值, // 定义一个属性
    // 函数名:function(){} // 定义一个函数
    // };

    var obj = {
      
      
    name:"国哥",
    age:18,
        fun : function () {
      
      
            alert("姓名:" + this.name + " , 年龄:" + this.age);
                    }
            };
                // 对象的访问:
                // 变量名.属性 / 函数名();
            alert(obj.name);
            obj.fun();
        </script>
    </head>
    <body>
    </body>
</html>

events in js

What is an event? Events are responses to computer input devices interacting with the page. We call them events.

Commonly used events:

  1. onload loading completion event: After the page is loaded, it is often used to initialize the page js code
  2. onclick Click event: Commonly used for button click response operations.
  3. onblur focus loss event: It is commonly used to verify whether the input content is legal after the input box loses focus.
  4. onchange Content change event: It is often used to operate after the content of the drop-down list and input box changes
  5. onsubmit Form submission event: It is often used to verify whether all form items are legal before the form is submitted.

Event registration is divided into static registration and dynamic registration:

​ What is event registration (binding)?

​ In fact, it is to tell the browser what operation codes to execute after the event is responded to, which is called event registration or event binding.

​ Static registration event:

​ The event attribute of the html tag is directly assigned to the code after the event response. This method is called static registration.

​ Dynamic registration event:

​ means to get the dom object of the label through js code first, and then pass

​ dom object.event name = function(){} This form is assigned to the code after the event response, which is called dynamic registration.

​ Basic steps of dynamic registration:

1. Get the label object

2. Label object.Event name = fucntion(){}

onload load complete

<!DOCTYPE html>
     <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Title</title>

        <script type="text/javascript">
            // onload 事件的方法
            function onloadFun() {
      
      
                alert('静态注册 onload 事件,所有代码');
                }
                // onload 事件动态注册。是固定写法
            window.onload = function () {
      
      
                alert("动态注册的 onload 事件");
                }
            </script>
        </head>
        <!--静态注册 onload 事件
        onload 事件是浏览器解析完页面之后就会自动触发的事件
        <body οnlοad="onloadFun();">-->
    <body>
    </body>
</html>

onclick click event

get label object

  • document is an object (document) provided by JavaScript language

    get get
    Element element (that is, label)
    By pass. . Depend on. . through. . .
    Id id attribute
    getElementById Get the tag object through the id attribute
<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script type="text/javascript">

    function onclickFun() {
      
      
            alert("静态注册 onclick 事件");
        }

    // 动态注册 onclick 事件

    window.onload = function () {
      
      

        // 1 获取标签对象
        /*
            * document 是 JavaScript 语言提供的一个对象(文档)<br/>
            * get 获取
            * Element 元素(就是标签)
            * By 通过。。 由。。经。。。
            * Id id 属性
            *
            * getElementById 通过 id 属性获取标签对象
            **/

        var btnObj = document.getElementById("btn01");

        // alert( btnObj );
        // 2 通过标签对象.事件名 = function(){}

        btnObj.onclick = function () {
      
      
        alert("动态注册的 onclick 事件");
            }
        }
    </script>
    </head>
        <body>
        <!--静态注册 onClick 事件-->
            <button onclick="onclickFun();">按钮 1</button>
            <button id="btn01">按钮 2</button>
        </body>
</html>

onchange content change event

<!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script type="text/javascript">
            function onchangeFun() {
      
      
                    alert("女神已经改变了");
                }
            window.onload = function () {
      
      

            //1 获取标签对象

            var selObj = document.getElementById("sel01");

            // alert( selObj );
            //2 通过标签对象.事件名 = function(){}

            selObj.onchange = function () {
      
      
                alert("男神已经改变了");
            }
        }
    </script>
</head>
        <body>
            请选择你心中的女神:
            <!--静态注册 onchange 事件-->
    <select onchange="onchangeFun();">

            <option>--女神--</option>
            <option>芳芳</option>
            <option>佳佳</option>
            <option>娘娘</option>
    </select>
            请选择你心中的男神:
            <select id="sel01">
            <option>--男神--</option>
            <option>国哥</option>
            <option>华仔</option>
            <option>富城</option>
    </select>
    </body>
</html>

onsubmit form submission event

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript">
        // 静态注册表单提交事务
        function onsubmitFun() {
      
      
            // 要验证所有表单项是否合法,如果,有一个不合法就阻止表单提交
            alert("静态注册表单提交事件----发现不合法");
            return flase;
        }
        window.onload = function () {
      
      
            //1 获取标签对象
            var formObj = document.getElementById("form01");
            //2 通过标签对象.事件名 = function(){}
            formObj.onsubmit = function () {
      
      
                // 要验证所有表单项是否合法,如果,有一个不合法就阻止表单提交
                alert("动态注册表单提交事件----发现不合法");
                return false;
            }
        }
    </script>
</head>

<body>
    <!--return false 可以阻止 表单提交 -->
    <form action="http://localhost:8080" method="get" onsubmit="return onsubmitFun();">
        <input type="submit" value="静态注册" />
    </form>
    <form action="http://localhost:8080" id="form01">
        <input type="submit" value="动态注册" />
    </form>
</body>

</html>

Guess you like

Origin blog.csdn.net/apple_67445472/article/details/131389843