Home Web Front-end JS Tutorial Detailed explanation of JavaScript event method examples

Detailed explanation of JavaScript event method examples

Jun 27, 2017 am 09:40 AM
javascript js event method

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
   <title></title>
   <style type="text/css">
       div{
           background-color:lightblue;
           margin:5px;
           padding:5px;

       }
   </style>
   <script src="Scripts/EventUtil.js?1.1.11" type="text/javascript"></script>
   <script type="text/javascript">
       var changeSize = function () {
           var oTxt = document.getElementById('txt');
           oTxt.size += 5;
       }

var showMessage = function () {
           alert('单击事件');
           alert('事件类型:' + event.type);
       }

window.onload = function () {
           document.getElementById('btn1').onclick =
               function () {
                   alert('btn1单击事件');
               }
       }
   </script>
</head>
<body>
   <input type="text" id="txt" size="10"/>
   <input type="button" value="加长" onclick="changeSize()"/>
   <input type="button" value="点我" onclick="showMessage()"/>
   <input type="button" id="btn1" value="按钮" />

<p> 只有三次抽奖机会! </p>
   <input id="btnLottery" type="button" value="抽奖"/>
   <div id="divResult">

</div>
<script type="text/javascript">
var times = 0; //Numbers
var arr = ['Ten million', 'A goddess', 'Bugatti Veyron ', 'Ocean view villa', 'All heroes and all skins', 'Unlimited recharge', 'Win every bet'];

var btnLottery = document.getElementById('btnLottery');
btnLottery. onclick = function () {
          if(times >= 3)
                                                                                                                                                 Return ;
                           times++; ');
divResult.innerHTML = "<p>You are the "+ times +" lottery draw, and the winner is: "+ arr[n] +"</p>"
  }
</script>

<input type="button" id="btnDOM2Test" value="Test DOM2 event processing"/>
<input type="button" id= "btnDelDOM2Test" value="Test DOM2 deletion event handling"/>
<script type="text/javascript">
var sayHi = function () {
            alert('Hello!') ;

    }


var btnDOM2Test = document.getElementById('btnDOM2Test');
        btnDOM2Test.addEventListener('click', function () {
              alert('You clicked: ' + this.value);
}, false);

btnDOM2Test.addEventListener('click', sayHi, false);


var btnDelDOM2Test = document.getElementById('btnDelDOM2Test' );

var del = function () {

btnDOM2Test.removeEventListener('click', function () {

              alert('You clicked: ' + this.value);

      }, false);

            btnDOM2Test.removeEventListener ('click', sayHi, false); /IE9+

                                          using using use using use using using using ‐ ‐                                       out out out’ ’ s ’ ‐ ‐ ‐ ‐ ‐ ‐‐ //btnDOM2Test.attachEvent("onclick", sayHi); ;

//});
</script>
##<label for="txtPhoneNum">Please enter your mobile phone number</label>
<input type= "text" id = "txtphonenum"/& gt;
& lt; script type = "text/javascript" & gt;
Var txtphonum = document.getelementByid ('txtphonenum' );
Eventutil.addhandler (txtphonenum, 'keypress', function () {

      var e = EventUtil.getEvent();

              //alert(e.keyCode); 48 || e.keyCode > 57) {
                EventUtil.preventDefault(e);
                                                                      using   using using using using   using   using using using   out out out out out out out of ’s ’ ’s to 57 through e.keyCode >  --EventUtil.preventDefault(e)- }

<form id="frmDemo" action="submitPage.htm">
       <h3>用户登录</h3>
       <label for="txtUID">账号:</label>
       <input type="text" id="txtUID"/><br />
       <label for="txtPWD">密码:</label>
       <input type="password" id="txtPWD"/><br />
       <input type="button" id="btnSubmit" value="登录"/>
   </form>

<script type="text/javascript">
       var txtUID = document.getElementById('txtUID');
       var txtPWD = document.getElementById('txtPWD');
       var btnSubmit = document.getElementById('btnSubmit');
       var frmDemo = document.getElementById('frmDemo');

EventUtil.addHandler(btnSubmit, 'click', function () {
           alert('我提交了!');
           if (txtUID.value == "admin") {
               frmDemo.submit();   //提交
           }
           else {
               return;
           }
       });

EventUtil.addHandler(txtUID, 'keydown', function () {
           var e = EventUtil.getEvent();   //Enter.keyCode = 13;
           if (e.keyCode == 13) {
               txtPWD.focus();
               EventUtil.preventDefault(e);
           }
       });
   </script>

<div onclick="this.style.backgroundColor='red'; event.cancelBubble=true;">div1
       <div onclick="this.style.backgroundColor='blue';">div2
           <div onclick="this.style.backgroundColor='black';">div3
           </div>
       </div>
   </div>
</body>
</html>

The above is the detailed content of Detailed explanation of JavaScript event method examples. 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 Article Tags

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)

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. Mar 28, 2024 pm 12:50 PM

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel.

How to enter bios on Colorful motherboard? Teach you two methods How to enter bios on Colorful motherboard? Teach you two methods Mar 13, 2024 pm 06:01 PM

How to enter bios on Colorful motherboard? Teach you two methods

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts)

Summary of methods to obtain administrator rights in Win11 Summary of methods to obtain administrator rights in Win11 Mar 09, 2024 am 08:45 AM

Summary of methods to obtain administrator rights in Win11

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Mar 23, 2024 am 10:42 AM

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed!

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs)

Detailed explanation of Oracle version query method Detailed explanation of Oracle version query method Mar 07, 2024 pm 09:21 PM

Detailed explanation of Oracle version query method

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

How to set font size on mobile phone (easily adjust font size on mobile phone)

See all articles