Home Web Front-end JS Tutorial Share an example code of JavaScript event

Share an example code of JavaScript event

Jun 27, 2017 am 09:39 AM
javascript js event Case

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
    /*
        window.onload = function () {
            alert(&#39;onload&#39;);
        }

        window.onfocus = function () {
            alert(&#39;onfocus&#39;);
        }

        window.onblur = function () {
            alert(&#39;onblur&#39;);
        }

        window.onscroll = function () {
            alert(&#39;onscroll&#39;);
        }

        window.onresize = function () {
            alert(&#39;onresize&#39;);
        }
        */
    </script>
    <script type="text/javascript">
        var msg = "";
        var myAlert = function () {
            msg += " Click Event End\n";
            alert(msg);
        }
    </script>
</head>
<body 
onclick="javascript:msg+=&#39;-->Body Event&#39;;myAlert();">
    <table border="1" 
    onclick="javascript:msg+=&#39;-->Table Event&#39;;myAlert();event.cancelBubble=true;">
    <tr 
    onclick="javascript:msg+=&#39;-->TR Event&#39;;myAlert();">
        <td 
        onclick="javascript:msg+=&#39;-->TD Event&#39;;myAlert();">
            我是单元格
            <p 
            onclick="javascript:msg+=&#39;-->P Event&#39;;myAlert();">
                我是段落
            </p>
        </td>
    </tr>
    </table>
</body>
</html>
Copy after login

  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/EventUtil.js?1.1.11" type="text/javascript"></script>
    <script type="text/javascript">
        var $ = function (id) {
            return document.getElementById(id);
        }

        var changeSize = function (id, size, obj) {
            var inp = $(id);
            obj.value += size;
            inp.size += size;
        }

        var onclickEvent = function () {
            alert("提交内容" + $(&#39;MyButton&#39;).value);
        }
    </script>
</head>
<body>
<input type="text" id="txt" size="15"/>
<input type="button" value="加长" onclick="changeSize(&#39;txt&#39;,30,this);"/>

<center>
<br>
<p>单击“事件测试”按钮,通过匿名函数处理事件</p>
<form name="myForm" id="myForm">
  <input type="button" name="myButton" id="myButton" value="事件测试">
</form>
<script type="text/javascript">
    /*
    $("myButton").onclick = function () {
        alert(&#39;myButton onclick&#39;);
    };
    */
    //$("myButton").onclick = onclickEvent;

    /*
    $("myButton").addEventListener(&#39;click&#39;
    , function () {
        alert("first click Event");
    }, false);

    $("myButton").addEventListener(&#39;click&#39;
    , function () {
        alert("second click Event");
    }, false);

    $("myButton").addEventListener(&#39;click&#39;
    , function () {
        alert("third click Event");
    }, false);
    */

    EventUtil.addHandler(
    $("myButton")
    ,&#39;click&#39;
    , function () {
        alert("I am compatible1 click Event");
    });

    EventUtil.addHandler(
    $("myButton")
    , &#39;click&#39;
    , function () {
        alert("I am compatible2 click Event");
    });
</script>
</center>

</body>
</html>
Copy after login
Copy after login

  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/EventUtil.js?1.1.11" type="text/javascript"></script>
    <script type="text/javascript">
        var $ = function (id) {
            return document.getElementById(id);
        }

        var changeSize = function (id, size, obj) {
            var inp = $(id);
            obj.value += size;
            inp.size += size;
        }

        var onclickEvent = function () {
            alert("提交内容" + $(&#39;MyButton&#39;).value);
        }
    </script>
</head>
<body>
<input type="text" id="txt" size="15"/>
<input type="button" value="加长" onclick="changeSize(&#39;txt&#39;,30,this);"/>

<center>
<br>
<p>单击“事件测试”按钮,通过匿名函数处理事件</p>
<form name="myForm" id="myForm">
  <input type="button" name="myButton" id="myButton" value="事件测试">
</form>
<script type="text/javascript">
    /*
    $("myButton").onclick = function () {
        alert(&#39;myButton onclick&#39;);
    };
    */
    //$("myButton").onclick = onclickEvent;

    /*
    $("myButton").addEventListener(&#39;click&#39;
    , function () {
        alert("first click Event");
    }, false);

    $("myButton").addEventListener(&#39;click&#39;
    , function () {
        alert("second click Event");
    }, false);

    $("myButton").addEventListener(&#39;click&#39;
    , function () {
        alert("third click Event");
    }, false);
    */

    EventUtil.addHandler(
    $("myButton")
    ,&#39;click&#39;
    , function () {
        alert("I am compatible1 click Event");
    });

    EventUtil.addHandler(
    $("myButton")
    , &#39;click&#39;
    , function () {
        alert("I am compatible2 click Event");
    });
</script>
</center>

</body>
</html>
Copy after login
Copy after login

  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
     .whiteStar  
     {
         float:left;
         width:64px;
         height:64px;
         background-image:url(/Images/starwhite.png)
     }
     .blueStar  
     {
         float:left;
         width:64px;
         height:64px;
         background-image:url(/Images/starblue.png)
     }
    </style>
    <script type="text/javascript">
        var $ = function (id) {
            return document.getElementById(id);
        }
    </script>
</head>
<body>
<div id="div1" class="whiteStar"></div>
<div id="div2" class="whiteStar"></div>
<div id="div3" class="whiteStar"></div>
<div id="div4" class="whiteStar"></div>
<div id="div5" class="whiteStar"></div>


<script type="text/javascript">
    //星星1
    $(&#39;div1&#39;).onmouseover = function () {
        $(&#39;div1&#39;).className = &#39;blueStar&#39;;
    };

    $(&#39;div1&#39;).onmouseout = function () {
        $(&#39;div1&#39;).className = &#39;whiteStar&#39;;
    };

    //星星2
    $(&#39;div2&#39;).onmouseover = function () {
        $(&#39;div1&#39;).className = &#39;blueStar&#39;;
        $(&#39;div2&#39;).className = &#39;blueStar&#39;;
    };

    $(&#39;div2&#39;).onmouseout = function () {
        $(&#39;div1&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div2&#39;).className = &#39;whiteStar&#39;;
    };

    //星星3
    $(&#39;div3&#39;).onmouseover = function () {
        $(&#39;div1&#39;).className = &#39;blueStar&#39;;
        $(&#39;div2&#39;).className = &#39;blueStar&#39;;
        $(&#39;div3&#39;).className = &#39;blueStar&#39;;
    };

    $(&#39;div3&#39;).onmouseout = function () {
        $(&#39;div1&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div2&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div3&#39;).className = &#39;whiteStar&#39;;
    };

    //星星4
    $(&#39;div4&#39;).onmouseover = function () {
        $(&#39;div1&#39;).className = &#39;blueStar&#39;;
        $(&#39;div2&#39;).className = &#39;blueStar&#39;;
        $(&#39;div3&#39;).className = &#39;blueStar&#39;;
        $(&#39;div4&#39;).className = &#39;blueStar&#39;;
    };

    $(&#39;div4&#39;).onmouseout = function () {
        $(&#39;div1&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div2&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div3&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div4&#39;).className = &#39;whiteStar&#39;;
    };

    //星星5
    $(&#39;div5&#39;).onmouseover = function () {
        $(&#39;div1&#39;).className = &#39;blueStar&#39;;
        $(&#39;div2&#39;).className = &#39;blueStar&#39;;
        $(&#39;div3&#39;).className = &#39;blueStar&#39;;
        $(&#39;div4&#39;).className = &#39;blueStar&#39;;
        $(&#39;div5&#39;).className = &#39;blueStar&#39;;
    };

    $(&#39;div5&#39;).onmouseout = function () {
        $(&#39;div1&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div2&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div3&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div4&#39;).className = &#39;whiteStar&#39;;
        $(&#39;div5&#39;).className = &#39;whiteStar&#39;;
    };

</script>
</body>
</html>
Copy after login

  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/EventUtil.js?1.1.11" type="text/javascript"></script>
    <script src="Scripts/KeyCodeList.js?1.1.11" type="text/javascript"></script>
</head>
<body>
    <input type="text" id="txt" />
    <script type="text/javascript">
        var objTxt = document.getElementById(&#39;txt&#39;);
        EventUtil.addHandler(objTxt, &#39;keypress&#39;, function () {
            var event = EventUtil.getEvent();
            alert(keyCodeList[event.keyCode]);
        });
    </script>
</body>
</html>
Copy after login

  

var keyCodeList = {
    65: &#39;A&#39;,
    66: &#39;B&#39;,
    67: &#39;C&#39;,
    68: &#39;D&#39;,
    69: &#39;E&#39;,
    70: &#39;F&#39;,
    71: &#39;G&#39;,
    72: &#39;H&#39;,
    73: &#39;I&#39;
    //...  
}
Copy after login

  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/EventUtil.js?1.1.11" type="text/javascript"></script>
    <script type="text/javascript">
        var doClick = function (id) {
            var btn = document.getElementById(id);
            alert(btn.value);
        }

        window.onload = function () {
            var oBtn1 = document.getElementById(&#39;btn1&#39;);
            EventUtil.addHandler(oBtn1, &#39;click&#39;,
            function () {
                alert(oBtn1.value)
            });

            //document.write(myClass.name);
            //document.write(myClass.name1());
            //myClass.action();
        }

        var myClass = {
            name: &#39;Nick.Chung&#39;,
            name1: function () {
                return &#39;Nick.Chung&#39;;
            },
            action: function () {
                alert(&#39;eating...&#39;);
            }
        };
    </script>
</head>
<body>
<input type="text" id="btn1" value="button1" />
<input type="button" id="btn1" value="button1" 
onclick = "doClick(&#39;btn1&#39;)"/>
<input type="button" id="btn3" value="button3" />
<input type="button" id="btn4" value="button4" />
<input type="button" id="btn5" value="button5" />
</body>
</html>
Copy after login

  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <!--引入兼容事件类-->
    <script src="Scripts/EventUtil.js?1.1.11" type="text/javascript"></script>
    <script type="text/javascript">
        //当HTML文档内容加载完成
        window.onload = function () {
            //获取按钮对象
            var oBtn =
            document.getElementById(&#39;btn&#39;);

            //获取复选框对象
            var oChk =
            document.getElementById(&#39;chk&#39;);

            //为复选框添加单击事件
            EventUtil.addHandler(oChk, &#39;click&#39;,
            function () {
                
                //如果复选框选中了
                if (oChk.checked) {
                    //禁用按钮
                    oBtn.disabled = true;
                }
                else {
                    //没选中则启动按钮
                    oBtn.disabled = false;
                }
            })


        }
    </script>
</head>
<body>
<input type="checkbox" id="chk" />禁用按钮
<br />
<br />
<input type="button" id="btn" 
disabled="disabled" value="我是按钮"/>
</body>
</html>
Copy after login

  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/EventUtil.js?1.1.11" type="text/javascript"></script>
    <script type="text/javascript">
        window.onload = function () {
            var oBtn1 = document.getElementById(&#39;btn1&#39;);
            var oBtn2 = document.getElementById(&#39;btn2&#39;);
            var oBtn3 = document.getElementById(&#39;btn3&#39;);
            var oBtn4 = document.getElementById(&#39;btn4&#39;);
            var oBtn5 = document.getElementById(&#39;btn5&#39;);

            EventUtil.addHandler(
            oBtn1,
            &#39;click&#39;,
            function () {
                var o = EventUtil.getEvent().srcElement;
                alert(o.value);
            });

            EventUtil.addHandler(
            oBtn2,
            &#39;click&#39;,
            function () {
                var o = EventUtil.getEvent().srcElement;
                alert(o.value);
            });

            EventUtil.addHandler(
            oBtn3,
            &#39;click&#39;,
            function () {
                var o = EventUtil.getEvent().srcElement;
                alert(o.value);
            });

            EventUtil.addHandler(
            oBtn4,
            &#39;click&#39;,
            function () {
                var o = EventUtil.getEvent().srcElement;
                alert(o.value);
            });

            EventUtil.addHandler(
            oBtn5,
            &#39;click&#39;,
            function () {
                var o = EventUtil.getEvent().srcElement;
                alert(o.value);
            });
        }
    </script>
</head>
<body>
<input type="button" id="btn1" value="button1" />
<input type="button" id="btn2" value="button2" />
<input type="button" id="btn3" value="button3" />
<input type="button" id="btn4" value="button4" />
<input type="button" id="btn5" value="button5" />
</body>
</html>
Copy after login

  

The above is the detailed content of Share an example code of JavaScript event. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

The relationship between js and vue The relationship between js and vue Mar 11, 2024 pm 05:21 PM

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

How to implement change event binding of select elements in jQuery How to implement change event binding of select elements in jQuery Feb 23, 2024 pm 01:12 PM

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

The AI ​​era of JS is here! The AI ​​era of JS is here! Apr 08, 2024 am 09:10 AM

Introduction to JS-Torch JS-Torch is a deep learning JavaScript library whose syntax is very similar to PyTorch. It contains a fully functional tensor object (can be used with tracked gradients), deep learning layers and functions, and an automatic differentiation engine. JS-Torch is suitable for deep learning research in JavaScript and provides many convenient tools and functions to accelerate deep learning development. Image PyTorch is an open source deep learning framework developed and maintained by Meta's research team. It provides a rich set of tools and libraries for building and training neural network models. PyTorch is designed to be simple, flexible and easy to use, and its dynamic computation graph features make

A Deep Dive into Close Button Events in jQuery A Deep Dive into Close Button Events in jQuery Feb 24, 2024 pm 05:09 PM

In-depth understanding of the close button event in jQuery During the front-end development process, we often encounter situations where we need to implement the close button function, such as closing pop-up windows, closing prompt boxes, etc. When using jQuery, a popular JavaScript library, it becomes extremely simple and convenient to implement the close button event. This article will delve into how to use jQuery to implement close button events, and provide specific code examples to help readers better understand and master this technology. First, we need to understand how to define

See all articles