Share an example code of JavaScript event
<!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('onload'); } window.onfocus = function () { alert('onfocus'); } window.onblur = function () { alert('onblur'); } window.onscroll = function () { alert('onscroll'); } window.onresize = function () { alert('onresize'); } */ </script> <script type="text/javascript"> var msg = ""; var myAlert = function () { msg += " Click Event End\n"; alert(msg); } </script> </head> <body onclick="javascript:msg+='-->Body Event';myAlert();"> <table border="1" onclick="javascript:msg+='-->Table Event';myAlert();event.cancelBubble=true;"> <tr onclick="javascript:msg+='-->TR Event';myAlert();"> <td onclick="javascript:msg+='-->TD Event';myAlert();"> 我是单元格 <p onclick="javascript:msg+='-->P Event';myAlert();"> 我是段落 </p> </td> </tr> </table> </body> </html>
<!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("提交内容" + $('MyButton').value); } </script> </head> <body> <input type="text" id="txt" size="15"/> <input type="button" value="加长" onclick="changeSize('txt',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('myButton onclick'); }; */ //$("myButton").onclick = onclickEvent; /* $("myButton").addEventListener('click' , function () { alert("first click Event"); }, false); $("myButton").addEventListener('click' , function () { alert("second click Event"); }, false); $("myButton").addEventListener('click' , function () { alert("third click Event"); }, false); */ EventUtil.addHandler( $("myButton") ,'click' , function () { alert("I am compatible1 click Event"); }); EventUtil.addHandler( $("myButton") , 'click' , function () { alert("I am compatible2 click Event"); }); </script> </center> </body> </html>
<!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("提交内容" + $('MyButton').value); } </script> </head> <body> <input type="text" id="txt" size="15"/> <input type="button" value="加长" onclick="changeSize('txt',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('myButton onclick'); }; */ //$("myButton").onclick = onclickEvent; /* $("myButton").addEventListener('click' , function () { alert("first click Event"); }, false); $("myButton").addEventListener('click' , function () { alert("second click Event"); }, false); $("myButton").addEventListener('click' , function () { alert("third click Event"); }, false); */ EventUtil.addHandler( $("myButton") ,'click' , function () { alert("I am compatible1 click Event"); }); EventUtil.addHandler( $("myButton") , 'click' , function () { alert("I am compatible2 click Event"); }); </script> </center> </body> </html>
<!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 $('div1').onmouseover = function () { $('div1').className = 'blueStar'; }; $('div1').onmouseout = function () { $('div1').className = 'whiteStar'; }; //星星2 $('div2').onmouseover = function () { $('div1').className = 'blueStar'; $('div2').className = 'blueStar'; }; $('div2').onmouseout = function () { $('div1').className = 'whiteStar'; $('div2').className = 'whiteStar'; }; //星星3 $('div3').onmouseover = function () { $('div1').className = 'blueStar'; $('div2').className = 'blueStar'; $('div3').className = 'blueStar'; }; $('div3').onmouseout = function () { $('div1').className = 'whiteStar'; $('div2').className = 'whiteStar'; $('div3').className = 'whiteStar'; }; //星星4 $('div4').onmouseover = function () { $('div1').className = 'blueStar'; $('div2').className = 'blueStar'; $('div3').className = 'blueStar'; $('div4').className = 'blueStar'; }; $('div4').onmouseout = function () { $('div1').className = 'whiteStar'; $('div2').className = 'whiteStar'; $('div3').className = 'whiteStar'; $('div4').className = 'whiteStar'; }; //星星5 $('div5').onmouseover = function () { $('div1').className = 'blueStar'; $('div2').className = 'blueStar'; $('div3').className = 'blueStar'; $('div4').className = 'blueStar'; $('div5').className = 'blueStar'; }; $('div5').onmouseout = function () { $('div1').className = 'whiteStar'; $('div2').className = 'whiteStar'; $('div3').className = 'whiteStar'; $('div4').className = 'whiteStar'; $('div5').className = 'whiteStar'; }; </script> </body> </html>
<!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('txt'); EventUtil.addHandler(objTxt, 'keypress', function () { var event = EventUtil.getEvent(); alert(keyCodeList[event.keyCode]); }); </script> </body> </html>
var keyCodeList = { 65: 'A', 66: 'B', 67: 'C', 68: 'D', 69: 'E', 70: 'F', 71: 'G', 72: 'H', 73: 'I' //... }
<!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('btn1'); EventUtil.addHandler(oBtn1, 'click', function () { alert(oBtn1.value) }); //document.write(myClass.name); //document.write(myClass.name1()); //myClass.action(); } var myClass = { name: 'Nick.Chung', name1: function () { return 'Nick.Chung'; }, action: function () { alert('eating...'); } }; </script> </head> <body> <input type="text" id="btn1" value="button1" /> <input type="button" id="btn1" value="button1" onclick = "doClick('btn1')"/> <input type="button" id="btn3" value="button3" /> <input type="button" id="btn4" value="button4" /> <input type="button" id="btn5" value="button5" /> </body> </html>
<!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('btn'); //获取复选框对象 var oChk = document.getElementById('chk'); //为复选框添加单击事件 EventUtil.addHandler(oChk, 'click', 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>
<!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('btn1'); var oBtn2 = document.getElementById('btn2'); var oBtn3 = document.getElementById('btn3'); var oBtn4 = document.getElementById('btn4'); var oBtn5 = document.getElementById('btn5'); EventUtil.addHandler( oBtn1, 'click', function () { var o = EventUtil.getEvent().srcElement; alert(o.value); }); EventUtil.addHandler( oBtn2, 'click', function () { var o = EventUtil.getEvent().srcElement; alert(o.value); }); EventUtil.addHandler( oBtn3, 'click', function () { var o = EventUtil.getEvent().srcElement; alert(o.value); }); EventUtil.addHandler( oBtn4, 'click', function () { var o = EventUtil.getEvent().srcElement; alert(o.value); }); EventUtil.addHandler( oBtn5, 'click', 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>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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: 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.

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

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:

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

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
